boon 0.6.1

JSONSchema (draft 2020-12, draft 2019-09, draft-7, draft-6, draft-4) Validation
Documentation
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
use std::{
    collections::{hash_map::Entry, HashMap},
    str::FromStr,
};

use once_cell::sync::Lazy;
use serde_json::{Map, Value};
use url::Url;

use crate::{compiler::*, root::Resource, util::*, SchemaIndex, Schemas};

const POS_SELF: u8 = 1 << 0;
const POS_PROP: u8 = 1 << 1;
const POS_ITEM: u8 = 1 << 2;

pub(crate) static DRAFT4: Lazy<Draft> = Lazy::new(|| Draft {
    version: 4,
    id: "id",
    url: "http://json-schema.org/draft-04/schema",
    subschemas: HashMap::from([
        // type agnostic
        ("definitions", POS_PROP),
        ("not", POS_SELF),
        ("allOf", POS_ITEM),
        ("anyOf", POS_ITEM),
        ("oneOf", POS_ITEM),
        // object
        ("properties", POS_PROP),
        ("additionalProperties", POS_SELF),
        ("patternProperties", POS_PROP),
        // array
        ("items", POS_SELF | POS_ITEM),
        ("additionalItems", POS_SELF),
        ("dependencies", POS_PROP),
    ]),
    vocab_prefix: "",
    all_vocabs: vec![],
    default_vocabs: vec![],
});

pub(crate) static DRAFT6: Lazy<Draft> = Lazy::new(|| {
    let mut subschemas = DRAFT4.subschemas.clone();
    subschemas.extend([("propertyNames", POS_SELF), ("contains", POS_SELF)]);
    Draft {
        version: 6,
        id: "$id",
        url: "http://json-schema.org/draft-06/schema",
        subschemas,
        vocab_prefix: "",
        all_vocabs: vec![],
        default_vocabs: vec![],
    }
});

pub(crate) static DRAFT7: Lazy<Draft> = Lazy::new(|| {
    let mut subschemas = DRAFT6.subschemas.clone();
    subschemas.extend([("if", POS_SELF), ("then", POS_SELF), ("else", POS_SELF)]);
    Draft {
        version: 7,
        id: "$id",
        url: "http://json-schema.org/draft-07/schema",
        subschemas,
        vocab_prefix: "",
        all_vocabs: vec![],
        default_vocabs: vec![],
    }
});

pub(crate) static DRAFT2019: Lazy<Draft> = Lazy::new(|| {
    let mut subschemas = DRAFT7.subschemas.clone();
    subschemas.extend([
        ("$defs", POS_PROP),
        ("dependentSchemas", POS_PROP),
        ("unevaluatedProperties", POS_SELF),
        ("unevaluatedItems", POS_SELF),
        ("contentSchema", POS_SELF),
    ]);
    Draft {
        version: 2019,
        id: "$id",
        url: "https://json-schema.org/draft/2019-09/schema",
        subschemas,
        vocab_prefix: "https://json-schema.org/draft/2019-09/vocab/",
        all_vocabs: vec![
            "core",
            "applicator",
            "validation",
            "meta-data",
            "format",
            "content",
        ],
        default_vocabs: vec!["core", "applicator", "validation"],
    }
});

pub(crate) static DRAFT2020: Lazy<Draft> = Lazy::new(|| {
    let mut subschemas = DRAFT2019.subschemas.clone();
    subschemas.extend([("prefixItems", POS_ITEM)]);
    Draft {
        version: 2020,
        id: "$id",
        url: "https://json-schema.org/draft/2020-12/schema",
        subschemas,
        vocab_prefix: "https://json-schema.org/draft/2020-12/vocab/",
        all_vocabs: vec![
            "core",
            "applicator",
            "unevaluated",
            "validation",
            "meta-data",
            "format-annotation",
            "format-assertion",
            "content",
        ],
        default_vocabs: vec!["core", "applicator", "unevaluated", "validation"],
    }
});

pub(crate) static STD_METASCHEMAS: Lazy<Schemas> =
    Lazy::new(|| load_std_metaschemas().expect("std metaschemas must be compilable"));

pub(crate) fn latest() -> &'static Draft {
    crate::Draft::default().internal()
}

// --

pub(crate) struct Draft {
    pub(crate) version: usize,
    pub(crate) url: &'static str,
    id: &'static str,                         // property name used to represent id
    subschemas: HashMap<&'static str, u8>,    // location of subschemas
    pub(crate) vocab_prefix: &'static str,    // prefix used for vocabulary
    pub(crate) all_vocabs: Vec<&'static str>, // names of supported vocabs
    pub(crate) default_vocabs: Vec<&'static str>, // names of default vocabs
}

impl Draft {
    pub(crate) fn from_url(url: &str) -> Option<&'static Draft> {
        let (mut url, frag) = split(url);
        if !frag.is_empty() {
            return None;
        }
        if let Some(s) = url.strip_prefix("http://") {
            url = s;
        }
        if let Some(s) = url.strip_prefix("https://") {
            url = s;
        }
        match url {
            "json-schema.org/schema" => Some(latest()),
            "json-schema.org/draft/2020-12/schema" => Some(&DRAFT2020),
            "json-schema.org/draft/2019-09/schema" => Some(&DRAFT2019),
            "json-schema.org/draft-07/schema" => Some(&DRAFT7),
            "json-schema.org/draft-06/schema" => Some(&DRAFT6),
            "json-schema.org/draft-04/schema" => Some(&DRAFT4),
            _ => None,
        }
    }

    fn get_schema(&self) -> Option<SchemaIndex> {
        let url = match self.version {
            2020 => "https://json-schema.org/draft/2020-12/schema",
            2019 => "https://json-schema.org/draft/2019-09/schema",
            7 => "http://json-schema.org/draft-07/schema",
            6 => "http://json-schema.org/draft-06/schema",
            4 => "http://json-schema.org/draft-04/schema",
            _ => return None,
        };
        let up = UrlPtr {
            url: Url::parse(url).unwrap_or_else(|_| panic!("{url} should be valid url")),
            ptr: "".into(),
        };
        STD_METASCHEMAS.get_by_loc(&up).map(|s| s.idx)
    }

    pub(crate) fn validate(&self, up: &UrlPtr, v: &Value) -> Result<(), CompileError> {
        let Some(sch) = self.get_schema() else {
            return Err(CompileError::Bug(
                format!("no metaschema preloaded for draft {}", self.version).into(),
            ));
        };
        STD_METASCHEMAS
            .validate(v, sch)
            .map_err(|src| CompileError::ValidationError {
                url: up.to_string(),
                src: src.clone_static(),
            })
    }

    fn get_id<'a>(&self, obj: &'a Map<String, Value>) -> Option<&'a str> {
        if self.version < 2019 && obj.contains_key("$ref") {
            return None; // All other properties in a "$ref" object MUST be ignored
        }
        let Some(Value::String(id)) = obj.get(self.id) else {
            return None;
        };
        let (id, _) = split(id); // ignore fragment
        Some(id).filter(|id| !id.is_empty())
    }

    pub(crate) fn get_vocabs(
        &self,
        url: &Url,
        doc: &Value,
    ) -> Result<Option<Vec<String>>, CompileError> {
        if self.version < 2019 {
            return Ok(None);
        }
        let Value::Object(obj) = doc else {
            return Ok(None);
        };

        let Some(Value::Object(obj)) = obj.get("$vocabulary") else {
            return Ok(None);
        };

        let mut vocabs = vec![];
        for (vocab, reqd) in obj {
            if let Value::Bool(true) = reqd {
                let name = vocab
                    .strip_prefix(self.vocab_prefix)
                    .filter(|name| self.all_vocabs.contains(name));
                if let Some(name) = name {
                    vocabs.push(name.to_owned()); // todo: avoid alloc
                } else {
                    return Err(CompileError::UnsupportedVocabulary {
                        url: url.as_str().to_owned(),
                        vocabulary: vocab.to_owned(),
                    });
                }
            }
        }
        Ok(Some(vocabs))
    }

    // collects anchors/dynamic_achors from `sch` into `res`.
    // note this does not collect from subschemas in sch.
    pub(crate) fn collect_anchors(
        &self,
        sch: &Value,
        sch_ptr: &JsonPointer,
        res: &mut Resource,
        url: &Url,
    ) -> Result<(), CompileError> {
        let Value::Object(obj) = sch else {
            return Ok(());
        };

        let mut add_anchor = |anchor: Anchor| match res.anchors.entry(anchor) {
            Entry::Occupied(entry) => {
                if entry.get() == sch_ptr {
                    // anchor with same root_ptr already exists
                    return Ok(());
                }
                Err(CompileError::DuplicateAnchor {
                    url: url.as_str().to_owned(),
                    anchor: entry.key().to_string(),
                    ptr1: entry.get().to_string(),
                    ptr2: sch_ptr.to_string(),
                })
            }
            entry => {
                entry.or_insert(sch_ptr.to_owned());
                Ok(())
            }
        };

        if self.version < 2019 {
            if obj.contains_key("$ref") {
                return Ok(()); // All other properties in a "$ref" object MUST be ignored
            }
            // anchor is specified in id
            if let Some(Value::String(id)) = obj.get(self.id) {
                let Ok((_, frag)) = Fragment::split(id) else {
                    let loc = UrlFrag::format(url, sch_ptr.as_str());
                    return Err(CompileError::ParseAnchorError { loc });
                };
                if let Fragment::Anchor(anchor) = frag {
                    add_anchor(anchor)?;
                };
                return Ok(());
            }
        }
        if self.version >= 2019 {
            if let Some(Value::String(anchor)) = obj.get("$anchor") {
                add_anchor(anchor.as_str().into())?;
            }
        }
        if self.version >= 2020 {
            if let Some(Value::String(anchor)) = obj.get("$dynamicAnchor") {
                add_anchor(anchor.as_str().into())?;
                res.dynamic_anchors.insert(anchor.as_str().into());
            }
        }
        Ok(())
    }

    // error is json-ptr to invalid id
    pub(crate) fn collect_resources(
        &self,
        sch: &Value,
        base: &Url,           // base of json
        sch_ptr: JsonPointer, // ptr of json
        url: &Url,
        resources: &mut HashMap<JsonPointer, Resource>,
    ) -> Result<(), CompileError> {
        if resources.contains_key(&sch_ptr) {
            // resources are already collected
            return Ok(());
        }
        if let Value::Bool(_) = sch {
            if sch_ptr.is_empty() {
                // root resource
                resources.insert(sch_ptr.clone(), Resource::new(sch_ptr, base.clone()));
            }
            return Ok(());
        }

        let Value::Object(obj) = sch else {
            return Ok(());
        };

        let mut base = base;
        let tmp;
        let res = if let Some(id) = self.get_id(obj) {
            let Ok(id) = UrlFrag::join(base, id) else {
                let loc = UrlFrag::format(url, sch_ptr.as_str());
                return Err(CompileError::ParseIdError { loc });
            };
            tmp = id.url;
            base = &tmp;
            Some(Resource::new(sch_ptr.clone(), base.clone()))
        } else if sch_ptr.is_empty() {
            // root resource
            Some(Resource::new(sch_ptr.clone(), base.clone()))
        } else {
            None
        };
        if let Some(res) = res {
            if let Some(dup) = resources.values_mut().find(|res| res.id == *base) {
                return Err(CompileError::DuplicateId {
                    url: url.to_string(),
                    id: base.to_string(),
                    ptr1: res.ptr.to_string(),
                    ptr2: dup.ptr.to_string(),
                });
            }
            resources.insert(sch_ptr.clone(), res);
        }

        // collect anchors into base resource
        if let Some(res) = resources.values_mut().find(|res| res.id == *base) {
            self.collect_anchors(sch, &sch_ptr, res, url)?;
        } else {
            debug_assert!(false, "base resource must exist");
        }

        for (&kw, &pos) in &self.subschemas {
            let Some(v) = obj.get(kw) else {
                continue;
            };
            if pos & POS_SELF != 0 {
                let ptr = sch_ptr.append(kw);
                self.collect_resources(v, base, ptr, url, resources)?;
            }
            if pos & POS_ITEM != 0 {
                if let Value::Array(arr) = v {
                    for (i, item) in arr.iter().enumerate() {
                        let ptr = sch_ptr.append2(kw, &i.to_string());
                        self.collect_resources(item, base, ptr, url, resources)?;
                    }
                }
            }
            if pos & POS_PROP != 0 {
                if let Value::Object(obj) = v {
                    for (pname, pvalue) in obj {
                        let ptr = sch_ptr.append2(kw, pname);
                        self.collect_resources(pvalue, base, ptr, url, resources)?;
                    }
                }
            }
        }
        Ok(())
    }

    pub(crate) fn is_subschema(&self, ptr: &str) -> bool {
        if ptr.is_empty() {
            return true;
        }

        fn split(mut ptr: &str) -> (&str, &str) {
            ptr = &ptr[1..]; // rm `/` prefix
            if let Some(i) = ptr.find('/') {
                (&ptr[..i], &ptr[i..])
            } else {
                (ptr, "")
            }
        }

        let (tok, ptr) = split(ptr);

        if let Some(&pos) = self.subschemas.get(tok) {
            if pos & POS_SELF != 0 && self.is_subschema(ptr) {
                return true;
            }
            if !ptr.is_empty() {
                if pos & POS_PROP != 0 {
                    let (_, ptr) = split(ptr);
                    if self.is_subschema(ptr) {
                        return true;
                    }
                }
                if pos & POS_ITEM != 0 {
                    let (tok, ptr) = split(ptr);
                    if usize::from_str(tok).is_ok() && self.is_subschema(ptr) {
                        return true;
                    }
                }
            }
        }

        false
    }
}

fn load_std_metaschemas() -> Result<Schemas, CompileError> {
    let mut schemas = Schemas::new();
    let mut compiler = Compiler::new();
    compiler.enable_format_assertions();
    compiler.compile("https://json-schema.org/draft/2020-12/schema", &mut schemas)?;
    compiler.compile("https://json-schema.org/draft/2019-09/schema", &mut schemas)?;
    compiler.compile("http://json-schema.org/draft-07/schema", &mut schemas)?;
    compiler.compile("http://json-schema.org/draft-06/schema", &mut schemas)?;
    compiler.compile("http://json-schema.org/draft-04/schema", &mut schemas)?;
    Ok(schemas)
}

#[cfg(test)]
mod tests {
    use crate::{Compiler, Schemas};

    use super::*;

    #[test]
    fn test_meta() {
        let mut schemas = Schemas::default();
        let mut compiler = Compiler::default();
        let v: Value = serde_json::from_str(include_str!("metaschemas/draft-04/schema")).unwrap();
        let url = "https://json-schema.org/draft-04/schema";
        compiler.add_resource(url, v).unwrap();
        compiler.compile(url, &mut schemas).unwrap();
    }

    #[test]
    fn test_from_url() {
        let tests = [
            ("http://json-schema.org/draft/2020-12/schema", Some(2020)), // http url
            ("https://json-schema.org/draft/2020-12/schema", Some(2020)), // https url
            ("https://json-schema.org/schema", Some(latest().version)),  // latest
            ("https://json-schema.org/draft-04/schema", Some(4)),
        ];
        for (url, version) in tests {
            let got = Draft::from_url(url).map(|d| d.version);
            assert_eq!(got, version, "for {url}");
        }
    }

    #[test]
    fn test_collect_ids() {
        let url = Url::parse("http://a.com/schema.json").unwrap();
        let json: Value = serde_json::from_str(
            r#"{
                "id": "http://a.com/schemas/schema.json",
                "definitions": {
                    "s1": { "id": "http://a.com/definitions/s1" },
                    "s2": {
                        "id": "../s2",
                        "items": [
                            { "id": "http://c.com/item" },
                            { "id": "http://d.com/item" }
                        ]
                    },
                    "s3": {
                        "definitions": {
                            "s1": {
                                "id": "s3",
                                "items": {
                                    "id": "http://b.com/item"
                                }
                            }
                        }
                    },
                    "s4": { "id": "http://e.com/def#abcd" }
                }
            }"#,
        )
        .unwrap();

        let want = {
            let mut m = HashMap::new();
            m.insert("", "http://a.com/schemas/schema.json"); // root with id
            m.insert("/definitions/s1", "http://a.com/definitions/s1");
            m.insert("/definitions/s2", "http://a.com/s2"); // relative id
            m.insert("/definitions/s3/definitions/s1", "http://a.com/schemas/s3");
            m.insert("/definitions/s3/definitions/s1/items", "http://b.com/item");
            m.insert("/definitions/s2/items/0", "http://c.com/item");
            m.insert("/definitions/s2/items/1", "http://d.com/item");
            m.insert("/definitions/s4", "http://e.com/def"); // id with fragments
            m
        };
        let mut got = HashMap::new();
        DRAFT4
            .collect_resources(&json, &url, "".into(), &url, &mut got)
            .unwrap();
        let got = got
            .iter()
            .map(|(k, v)| (k.as_str(), v.id.as_str()))
            .collect::<HashMap<&str, &str>>();
        assert_eq!(got, want);
    }

    #[test]
    fn test_collect_anchors() {
        let url = Url::parse("http://a.com/schema.json").unwrap();
        let json: Value = serde_json::from_str(
            r#"{
                "$defs": {
                    "s2": {
                        "$id": "http://b.com",
                        "$anchor": "b1", 
                        "items": [
                            { "$anchor": "b2" },
                            {
                                "$id": "http//c.com",
                                "items": [
                                    {"$anchor": "c1"},
                                    {"$dynamicAnchor": "c2"}
                                ]
                            },
                            { "$dynamicAnchor": "b3" }
                        ]
                    }
                }
            }"#,
        )
        .unwrap();
        let mut resources = HashMap::new();
        DRAFT2020
            .collect_resources(&json, &url, "".into(), &url, &mut resources)
            .unwrap();
        assert!(resources.get("").unwrap().anchors.is_empty());
        assert_eq!(resources.get("/$defs/s2").unwrap().anchors, {
            let mut want = HashMap::new();
            want.insert("b1".into(), "/$defs/s2".into());
            want.insert("b2".into(), "/$defs/s2/items/0".into());
            want.insert("b3".into(), "/$defs/s2/items/2".into());
            want
        });
        assert_eq!(resources.get("/$defs/s2/items/1").unwrap().anchors, {
            let mut want = HashMap::new();
            want.insert("c1".into(), "/$defs/s2/items/1/items/0".into());
            want.insert("c2".into(), "/$defs/s2/items/1/items/1".into());
            want
        });
    }

    #[test]
    fn test_is_subschema() {
        let tests = vec![("/allOf/0", true), ("/allOf/$defs", false)];
        for test in tests {
            let got = DRAFT2020.is_subschema(test.0);
            assert_eq!(got, test.1, "{}", test.0);
        }
    }
}