-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSONSchemaDraft2020.swift
776 lines (751 loc) · 30.4 KB
/
JSONSchemaDraft2020.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//
// JSONSchemaDraft2020.swift
// DynamicJSONTests
//
// Created by Matthias Zenger on 22/03/2024.
// Copyright © 2024 Matthias Zenger. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
///
/// Implementation of JSON schema validation based on the JSON schema Draft 2020-12
/// standard. The validator is configured via its `Dialect` descriptor which uses
/// a `Vocabulary` value to declare what vocaularies of the schema are being
/// evaluated.
///
/// Two standard dialects are predefined `JSONSchemaDraft2020.Dialect.default` and
/// `JSONSchemaDraft2020.Dialect.validateFormat` (this one enables the
/// `format-annotation` vocabulary). These are also available as
/// `draft2020` and `draft2020Format` values via protocol `JSONSchemaDialect`.
///
///
///
open class JSONSchemaDraft2020: JSONSchemaValidator {
/// Draft 2020-20 vocabulary implementation
public struct Vocabulary {
public let core: Bool
public let applicator: Bool
public let unevaluated: Bool
public let validation: Bool
public let metadata: Bool
public let format: Bool
public let content: Bool
public let deprecated: Bool
public let formatValidators: [String : (String) -> Bool]
public init(core: Bool = true,
applicator: Bool = true,
unevaluated: Bool = true,
validation: Bool = true,
metadata: Bool = true,
format: Bool = false,
content: Bool = true,
deprecated: Bool = true,
formatValidators: [String : (String) -> Bool] = JSONSchemaFormatValidators.draft2020) {
self.core = core
self.applicator = applicator
self.unevaluated = unevaluated
self.validation = validation
self.metadata = metadata
self.format = format
self.content = content
self.deprecated = deprecated
self.formatValidators = formatValidators
}
}
/// Draft 2020-20 dialect representation
public struct Dialect: JSONSchemaDialect {
public static let `default`: Dialect = Dialect()
public static let `validateFormat`: Dialect = Dialect(vocabulary: Vocabulary(format: true))
public let uri: URL
public let vocabulary: Vocabulary
public init(uri: URL = URL(string: "https://json-schema.org/draft/2020-12/schema")!,
vocabulary: Vocabulary = Vocabulary()) {
self.uri = uri
self.vocabulary = vocabulary
}
public func validator(for schema: JSONSchema,
in context: JSONSchemaValidationContext) -> JSONSchemaValidator {
return JSONSchemaDraft2020(dialect: self, context: context, schema: schema)
}
}
/// Draft 2020-20 error reasons
public enum Reason: FailureReason, CustomStringConvertible {
case validationError(Error)
case alwaysFails
case schemaValidatesButShouldFail
case noneOfTheSchemaValidates
case tooManySchemaValidate
case notIntMultipleOf(Int64, Double)
case notFloatMultipleOf(Double, Double)
case exceedsIntMaximum(Int64, Double, Bool)
case exceedsFloatMaximum(Double, Double, Bool)
case belowIntMinimum(Int64, Double, Bool)
case belowFloatMinimum(Double, Double, Bool)
case invalidPattern(String?, String)
case patternNotMatching(String, String)
case tooManyArrayItems(UInt, UInt)
case tooFewArrayItems(UInt, UInt)
case itemsNotUnique
case tooManyProperties(UInt, UInt)
case tooFewProperties(UInt, UInt)
case propertiesMissing([String])
case dependentPropertiesMissing(String, [String])
case valueNotConst
case valueNotFoundInEnum
case invalidType(JSONType, JSONType)
case exceedsMaxLength(String, UInt)
case lessThanMinLength(String, UInt)
case containCountMismatch(UInt, UInt, UInt)
case arrayPrefixInvalid(Int)
case arrayItemInvalid(Int)
case formatMismatch(String)
public var reason: String {
switch self {
case .validationError(let error):
return "Validation error: \(error.localizedDescription)"
case .alwaysFails:
return "Schema always fails validation"
case .noneOfTheSchemaValidates:
return "None of the schema validates"
case .schemaValidatesButShouldFail:
return "Schema validates but should fail"
case .tooManySchemaValidate:
return "More than one schema validates"
case .exceedsIntMaximum(let x, let max, let excl):
return "Integer number \(x) exceeds \(excl ? "exclusive " : "")" +
"maximum value of \(max)."
case .exceedsFloatMaximum(let x, let max, let excl):
return "Floating-point number \(x) exceeds \(excl ? "exclusive " : "")" +
"maximum value of \(max)."
case .belowIntMinimum(let x, let min, let excl):
return "Integer number \(x) below \(excl ? "exclusive " : "")minimum value of \(min)."
case .belowFloatMinimum(let x, let min, let excl):
return "Floating-point number \(x) below \(excl ? "exclusive " : "")" +
"minimum value of \(min)."
case .notIntMultipleOf(let x, let multipleOf):
return "Integer number \(x) is not a mutiple of \(multipleOf)"
case .notFloatMultipleOf(let x, let multipleOf):
return "Floating-point number \(x) is not a mutiple of \(multipleOf)"
case .invalidPattern(let str, let pattern):
if let str {
return "Could not validate string '\(str)' with erroneous pattern '\(pattern)'"
} else {
return "Erroneous regular-expression pattern '\(pattern)'"
}
case .patternNotMatching(let str, let pattern):
return "String '\(str)' does not match pattern '\(pattern)'"
case .tooManyArrayItems(let count, let max):
return "Array item count \(count) is more than maximum count of \(max)"
case .tooFewArrayItems(let count, let min):
return "Array item count \(count) is less than minimum count of \(min)"
case .itemsNotUnique:
return "Array items not unique"
case .tooManyProperties(let count, let max):
return "Object property count \(count) is more than maximum count of \(max)"
case .tooFewProperties(let count, let min):
return "Object property count \(count) is less than minimum count of \(min)"
case .propertiesMissing(let props):
return "Missing required property: \(props.joined(separator: ", "))"
case .dependentPropertiesMissing(let prop, let others):
return "Dependencies for property '\(prop)' failed. " +
"Missing required properties: \(others.joined(separator: ", "))"
case .valueNotConst:
return "Value does not match const"
case .valueNotFoundInEnum:
return "Value not found in enum"
case .invalidType(let expected, let found):
return "Invalid type; expected \(expected) but found \(found)"
case .exceedsMaxLength(let str, let maxLength):
return "String '\(str)' exceeds maximum length of \(maxLength)"
case .lessThanMinLength(let str, let minLength):
return "String '\(str)' is less than minimum length of \(minLength)"
case .containCountMismatch(let matches, let min, let max):
return "Contains match count \(matches) is outside the required range [\(min); \(max)]"
case .arrayPrefixInvalid(let index):
return "Array item at index \(index) does not match the schema prefix"
case .arrayItemInvalid(let index):
return "Array item at index \(index) does not match the required schema"
case .formatMismatch(let format):
return "String does not match format '\(format)'"
}
}
public var description: String {
return self.reason
}
}
/// The dialect defining what is being evaluated by this validator.
public let dialect: Dialect
/// The validation context.
public let context: JSONSchemaValidationContext
/// The schema being currently validated.
public let schema: JSONSchema
/// Initializer of a new validator object. This initialized should not be called in
/// custom code. It is used primarily by the validator factory method provided by
/// the "Draft 2020-12" dialect value.
public init(dialect: Dialect, context: JSONSchemaValidationContext, schema: JSONSchema) {
self.dialect = dialect
self.context = context
self.schema = schema
}
open func validateCore(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
guard self.dialect.vocabulary.core,
case .descriptor(let descriptor, _) = self.schema else {
return
}
func flag(_ reason: Reason, for member: String) {
result.flag(error: reason, for: instance, schema: self.schema, at: .member(self.context.location, member))
}
// if let id = descriptor.id {}
// if let schema = descriptor.schema {}
// if let anchor = descriptor.anchor {}
if let ref = descriptor.ref {
do {
let validator = try self.context.validator(for: ref,
at: .member(self.context.location, "$ref"),
dialect: dialect)
result.include(validator.validate(instance))
} catch let e {
flag(.validationError(e), for: "$ref")
}
}
if let dynamicRef = descriptor.dynamicRef {
do {
let validator = try self.context.validator(for: dynamicRef,
at: .member(self.context.location, "$dynamicRef"),
dynamic: true,
dialect: dialect)
result.include(validator.validate(instance))
} catch let e {
flag(.validationError(e), for: "$dynamicRef")
}
}
// if let dynamicAnchor = descriptor.dynamicAnchor {}
// if let vocabulary = descriptor.vocabulary {
//
// }
// if let comment = descriptor.comment {}
// if let defs = descriptor.defs {}
}
open func validateApplicator(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
guard self.dialect.vocabulary.applicator,
case .descriptor(let descriptor, _) = self.schema else {
return
}
func flag(_ reason: Reason, for member: String) {
result.flag(error: reason, for: instance, schema: self.schema, at: .member(self.context.location, member))
}
func validator(for schema: JSONSchema,
at member: String, _ member2: String? = nil,
index: Int? = nil) -> JSONSchemaValidator? {
do {
var location: JSONLocation = .member(self.context.location, member)
if let member2 {
location = .member(location, member2)
}
if let index, index >= 0 {
location = .index(location, index)
}
return try self.context.validator(for: schema, at: location, dialect: self.dialect)
} catch let e {
flag(.validationError(e), for: member)
return nil
}
}
if let prefixItems = descriptor.prefixItems, case .array(let arr) = instance.value {
do {
let location: JSONLocation = .member(self.context.location, "prefixItems")
for i in 0..<min(prefixItems.count, arr.count) {
let validator = try self.context.validator(for: prefixItems[i],
at: .index(location, i),
dialect: self.dialect)
if let item = instance.index(i) {
result.include(validator.validate(item), for: i)
}
}
} catch let e {
flag(.validationError(e), for: "prefixItems")
}
}
if let items = descriptor.items, case .array(let arr) = instance.value {
let start = descriptor.prefixItems?.count ?? 0
if start < arr.count, let validator = validator(for: items, at: "items") {
for i in start..<arr.count {
if let item = instance.index(i) {
result.include(validator.validate(item), for: i)
}
}
}
}
if let contains = descriptor.contains, case .array(let arr) = instance.value {
if let validator = validator(for: contains, at: "contains") {
var numContains: UInt = 0
for i in arr.indices {
if let item = instance.index(i) {
if result.include(ifValid: validator.validate(item), for: i) {
numContains += 1
}
}
}
let maxContains = descriptor.maxContains ?? .max
let minContains = descriptor.minContains ?? 1
if numContains < minContains || numContains > maxContains {
flag(.containCountMismatch(numContains, minContains, maxContains), for: "contains")
}
}
}
if case .object(let d) = instance.value {
var validatedMembers: Set<String> = []
if let properties = descriptor.properties {
for (member, schema) in properties {
if let validator = validator(for: schema, at: "properties", member) {
// If value defines this member, validate it
if let value = instance.member(member) {
validatedMembers.insert(member)
result.include(validator.validate(value), for: member)
// If value does not define this member, compute defaults
} else {
let val = LocatedJSON(.null, instance.location.select(member: member), exists: false)
let res = validator.validate(val)
result.merge(defaults: res.defaults, mode: .merge)
}
}
}
}
if let patternProperties = descriptor.patternProperties {
for (pattern, schema) in patternProperties {
if let expr = try? NSRegularExpression(pattern: pattern) {
for m in d.keys {
if expr.matches(in: m, options: .init(), range: NSMakeRange(0, m.count)).count > 0,
let value = instance.member(m),
let validator = validator(for: schema, at: "properties", m) {
validatedMembers.insert(m)
result.include(validator.validate(value), for: m)
}
}
} else {
flag(.invalidPattern(nil, pattern), for: "pattern")
}
}
}
if let additionalProperties = descriptor.additionalProperties,
let validator = validator(for: additionalProperties, at: "additionalProperties") {
for m in d.keys where !validatedMembers.contains(m) {
if let value = instance.member(m) {
result.include(validator.validate(value), for: m)
}
}
}
}
if let dependentSchemas = descriptor.dependentSchemas, case .object(let d) = instance.value {
for (member, schema) in dependentSchemas where d[member] != nil {
if let validator = validator(for: schema, at: "dependentSchemas", member) {
result.include(validator.validate(instance))
}
}
}
if let propertyNames = descriptor.propertyNames,
case .object(let dict) = instance.value,
let validator = validator(for: propertyNames, at: "propertyNames") {
for member in dict.keys {
result.include(validator.validate(LocatedJSON(root: .string(member))))
}
}
if let `if` = descriptor.if, let condition = validator(for: `if`, at: "if") {
if result.include(ifValid: condition.validate(instance), propagateDefault: .suppress) {
if let then = descriptor.then, let validator = validator(for: then, at: "then") {
result.include(validator.validate(instance))
}
} else if let `else` = descriptor.else, let validator = validator(for: `else`, at: "else") {
result.include(validator.validate(instance))
}
}
if let allOf = descriptor.allOf {
for i in allOf.indices {
if let validator = validator(for: allOf[i], at: "allOf", index: i) {
result.include(validator.validate(instance))
}
}
}
if let anyOf = descriptor.anyOf {
var valid = false
for i in anyOf.indices {
if let validator = validator(for: anyOf[i], at: "anyOf", index: i) {
if result.include(ifValid: validator.validate(instance), propagateDefault: .altenative) {
valid = true
}
}
}
if !valid {
flag(.noneOfTheSchemaValidates, for: "anyOf")
}
}
if let oneOf = descriptor.oneOf {
var valid = false
var flagged = false
for i in oneOf.indices {
if let validator = validator(for: oneOf[i], at: "oneOf", index: i) {
if result.include(ifValid: validator.validate(instance), propagateDefault: .altenative) {
if valid && !flagged {
flag(.tooManySchemaValidate, for: "oneOf")
flagged = true
}
valid = true
}
}
}
if !valid {
flag(.noneOfTheSchemaValidates, for: "anyOf")
}
}
if let not = descriptor.not, let validator = validator(for: not, at: "not") {
if result.include(ifValid: validator.validate(instance), propagateDefault: .suppress) {
flag(.schemaValidatesButShouldFail, for: "not")
}
}
}
open func validateUnevaluated(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
guard self.dialect.vocabulary.unevaluated,
case .descriptor(let descriptor, _) = self.schema else {
return
}
func flag(_ reason: Reason, for member: String) {
result.flag(error: reason, for: instance, schema: self.schema, at: .member(self.context.location, member))
}
func validator(for schema: JSONSchema,
at member: String, _ member2: String? = nil,
index: Int? = nil) -> JSONSchemaValidator? {
do {
var location: JSONLocation = .member(self.context.location, member)
if let member2 {
location = .member(location, member2)
}
if let index, index >= 0 {
location = .index(location, index)
}
return try self.context.validator(for: schema, at: location, dialect: self.dialect)
} catch let e {
flag(.validationError(e), for: member)
return nil
}
}
if let unevaluatedProperties = descriptor.unevaluatedProperties,
case .object(let dict) = instance.value,
let validator = validator(for: unevaluatedProperties, at: "unevaluatedProperties") {
for member in dict.keys where !result.evaluatedProperties.contains(member) {
if let value = instance.member(member) {
result.include(validator.validate(value), for: member)
}
}
}
if let unevaluatedItems = descriptor.unevaluatedItems,
case .array(let arr) = instance.value,
let validator = validator(for: unevaluatedItems, at: "unevaluatedItems") {
for i in arr.indices where !result.evaluatedItems.contains(i) {
if let value = instance.index(i) {
result.include(validator.validate(value), for: i)
}
}
}
}
open func validateValidation(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
guard self.dialect.vocabulary.validation,
case .descriptor(let descriptor, _) = self.schema else {
return
}
func flag(_ reason: Reason, for member: String) {
result.flag(error: reason, for: instance, schema: self.schema, at: .member(self.context.location, member))
}
if let multipleOf = descriptor.multipleOf {
switch instance.value {
case .integer(let x):
if let y = Int64(exactly: multipleOf), x % y != 0 {
flag(.notIntMultipleOf(x, multipleOf), for: "multipleOf")
} else {
let y = Double(x) / multipleOf
if Foundation.trunc(y) != y {
flag(.notIntMultipleOf(x, multipleOf), for: "multipleOf")
}
}
case .float(let x):
let y = x / multipleOf
if Foundation.trunc(y) != y {
flag(.notFloatMultipleOf(x, multipleOf), for: "multipleOf")
}
default:
break
}
}
if let max = descriptor.maximum {
switch instance.value {
case .integer(let x):
if let y = Int64(exactly: max), x > y {
flag(.exceedsIntMaximum(x, max, false), for: "maximum")
} else if Double(x) > max {
flag(.exceedsIntMaximum(x, max, false), for: "maximum")
}
case .float(let x):
if x > max {
flag(.exceedsFloatMaximum(x, max, false), for: "maximum")
}
default:
break
}
}
if let max = descriptor.exclusiveMaximum {
switch instance.value {
case .integer(let x):
if let y = Int64(exactly: max), x >= y {
flag(.exceedsIntMaximum(x, max, true), for: "exclusiveMaximum")
} else if Double(x) >= max {
flag(.exceedsIntMaximum(x, max, true), for: "exclusiveMaximum")
}
case .float(let x):
if x >= max {
flag(.exceedsFloatMaximum(x, max, true), for: "exclusiveMaximum")
}
default:
break
}
}
if let min = descriptor.minimum {
switch instance.value {
case .integer(let x):
if let y = Int64(exactly: min), x < y {
flag(.belowIntMinimum(x, min, false), for: "minimum")
} else if Double(x) < min {
flag(.belowIntMinimum(x, min, false), for: "minimum")
}
case .float(let x):
if x < min {
flag(.belowFloatMinimum(x, min, false), for: "minimum")
}
default:
break
}
}
if let min = descriptor.exclusiveMinimum {
switch instance.value {
case .integer(let x):
if let y = Int64(exactly: min), x <= y {
flag(.belowIntMinimum(x, min, true), for: "exclusiveMinimum")
} else if Double(x) <= min {
flag(.belowIntMinimum(x, min, true), for: "exclusiveMinimum")
}
case .float(let x):
if x <= min {
flag(.belowFloatMinimum(x, min, true), for: "exclusiveMinimum")
}
default:
break
}
}
if let maxLength = descriptor.maxLength,
case .string(let str) = instance.value,
str.count > maxLength {
flag(.exceedsMaxLength(str, maxLength), for: "maxLength")
}
if let minLength = descriptor.minLength,
case .string(let str) = instance.value,
str.count < minLength {
flag(.lessThanMinLength(str, minLength), for: "minLength")
}
if let pattern = descriptor.pattern, case .string(let str) = instance.value {
if let expr = try? NSRegularExpression(pattern: pattern) {
if expr.matches(in: str, options: .init(), range: NSMakeRange(0, str.count)).count == 0 {
flag(.patternNotMatching(str, pattern), for: "pattern")
}
} else {
flag(.invalidPattern(str, pattern), for: "pattern")
}
}
if let max = descriptor.maxItems, case .array(let arr) = instance.value, arr.count > max {
flag(.tooManyArrayItems(UInt(arr.count), max), for: "maxItems")
}
if let min = descriptor.minItems, case .array(let arr) = instance.value, arr.count < min {
flag(.tooFewArrayItems(UInt(arr.count), min), for: "minItems")
}
if let unique = descriptor.uniqueItems, unique, case .array(let arr) = instance.value {
var set: Set<JSON> = []
for value in arr {
if set.contains(value) {
flag(.itemsNotUnique, for: "uniqueItems")
break
} else {
set.insert(value)
}
}
}
if let max = descriptor.maxProperties, case .object(let d) = instance.value, d.count > max {
flag(.tooManyProperties(UInt(d.count), max), for: "maxProperties")
}
if let min = descriptor.minProperties, case .object(let d) = instance.value, d.count < min {
flag(.tooFewProperties(UInt(d.count), min), for: "minProperties")
}
if let required = descriptor.required, case .object(let dict) = instance.value {
let missing = required.filter { dict[$0] == nil }
if !missing.isEmpty {
flag(.propertiesMissing(missing), for: "required")
}
}
if let depRequired = descriptor.dependentRequired, case .object(let dict) = instance.value {
for (m, others) in depRequired where dict[m] != nil {
let miss = others.filter { dict[$0] == nil }
if !miss.isEmpty {
flag(.dependentPropertiesMissing(m, miss), for: "dependentRequired")
}
}
}
if let const = descriptor.const, const != instance.value {
flag(.valueNotConst, for: "const")
}
if let `enum` = descriptor.enum, !`enum`.contains(instance.value) {
flag(.valueNotFoundInEnum, for: "enum")
}
if let exp = descriptor.type, !instance.value.type.included(in: exp) {
flag(.invalidType(exp, instance.value.type), for: "type")
}
}
open func validateMetadata(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
guard self.dialect.vocabulary.metadata,
case .descriptor(let descriptor, _) = self.schema else {
return
}
if let `default` = descriptor.default {
result.flag(default: `default`, for: instance, schema: self.schema, at: self.context.location)
}
var tags = JSONSchemaValidationResult.MetaTags()
if let deprecated = descriptor.deprecated, deprecated {
tags.insert(.deprecated)
}
if let readOnly = descriptor.readOnly, readOnly {
tags.insert(.readOnly)
}
if let writeOnly = descriptor.writeOnly, writeOnly {
tags.insert(.writeOnly)
}
if !tags.isEmpty {
result.flag(tags: tags, for: instance, schema: self.schema, at: self.context.location)
}
}
open func validateFormat(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
guard case .descriptor(let descriptor, _) = self.schema,
let format = descriptor.format,
case .string(let str) = instance.value else {
return
}
if self.dialect.vocabulary.format,
let validator = self.dialect.vocabulary.formatValidators[format] {
let valid = validator(str)
if !valid {
result.flag(error: Reason.formatMismatch(format),
for: instance,
schema: self.schema,
at: .member(self.context.location, "format"))
}
// Write annotation
result.flag(format: format,
valid: valid,
for: instance,
schema: self.schema,
at: .member(self.context.location, "format"))
} else {
// Write annotation
result.flag(format: format,
valid: nil,
for: instance,
schema: self.schema,
at: .member(self.context.location, "format"))
}
}
open func validateContent(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
guard self.dialect.vocabulary.content,
case .descriptor(_, _) = self.schema else {
return
}
}
open func validateDeprecated(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
guard self.dialect.vocabulary.deprecated,
case .descriptor(let descriptor, _) = self.schema else {
return
}
func flag(_ reason: Reason, for member: String) {
result.flag(error: reason,
for: instance,
schema: self.schema,
at: .member(self.context.location, member))
}
func validator(for schema: JSONSchema,
at member: String, _ member2: String? = nil,
index: Int? = nil) -> JSONSchemaValidator? {
do {
var location: JSONLocation = .member(self.context.location, member)
if let member2 {
location = .member(location, member2)
}
if let index, index >= 0 {
location = .index(location, index)
}
return try self.context.validator(for: schema, at: location, dialect: self.dialect)
} catch let e {
flag(.validationError(e), for: member)
return nil
}
}
if let dependencies = descriptor.dependencies, case .object(let d) = instance.value {
for (member, mode) in dependencies where d[member] != nil {
switch mode {
case .array(let arr):
let miss = arr.filter { d[$0] == nil }
if !miss.isEmpty {
flag(.dependentPropertiesMissing(member, miss), for: "dependencies")
}
case .schema(let schema):
if let validator = validator(for: schema, at: "dependencies", member) {
result.include(validator.validate(instance))
}
}
}
}
}
/// Validate all the vocabularies for the given instance, writing annotations into the
/// validation results object `result`.
open func validate(instance: LocatedJSON, result: inout JSONSchemaValidationResult) {
self.validateCore(instance: instance, result: &result)
self.validateApplicator(instance: instance, result: &result)
self.validateValidation(instance: instance, result: &result)
self.validateMetadata(instance: instance, result: &result)
self.validateFormat(instance: instance, result: &result)
self.validateContent(instance: instance, result: &result)
self.validateUnevaluated(instance: instance, result: &result)
self.validateDeprecated(instance: instance, result: &result)
}
/// Validate the given instance returning a new validation results value.
open func validate(_ instance: LocatedJSON) -> JSONSchemaValidationResult {
var result = JSONSchemaValidationResult(for: instance.location)
// Check if this schema always suceeds or fails
if case .boolean(let bool) = self.schema {
if !bool {
result.flag(error: Reason.alwaysFails,
for: instance,
schema: self.schema,
at: self.context.location)
}
return result
}
// Validate supported vocabularies
self.validate(instance: instance, result: &result)
// Return result
return result
}
}