Skip to content

Commit e5d9932

Browse files
committed
Multiple schemata to validator support
1 parent e1cf0b6 commit e5d9932

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

sitelib/text/json/schema/validators.scm

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
(let ((r (validator v ctx)))
7878
(or (and lint-mode?
7979
(cond ((*json-schema:validator-error-reporter*) =>
80-
(lambda (reporter) (reporter ctx) #f))
80+
(lambda (reporter) (reporter ctx) r))
8181
(else r)))
8282
r))))
8383

@@ -101,9 +101,23 @@
101101
(lambda (validator source version id)
102102
((n (run-validator validator)) source version id)))))
103103

104-
(define (json-schema->json-validator schema . referencing-validators)
104+
(define (json-schema->json-validator schema . dependency-schemata)
105+
(define (ensure-schema schema/validator)
106+
(cond ((json-schema-validator? schema/validator)
107+
(json-schema-validator-source schema/validator))
108+
((json-schema? schema/validator) schema/validator)
109+
(else
110+
(assertion-violation 'json-schema->json-validator
111+
"dependency must be JSON Schema or json-schema-validator"
112+
dependency-schemata))))
113+
(define (compile-dependency schema context)
114+
(let ((dependency-context (make-disjoint-context schema context)))
115+
(initial-schema-context->schema-validator dependency-context)))
116+
105117
(let* ((root (make-root-context *version-specifics*))
106118
(context (make-initial-schema-context schema root)))
119+
(for-each (lambda (schema) (compile-dependency schema context))
120+
(map ensure-schema dependency-schemata))
107121
(make-json-schema-validator
108122
(schema-validator->core-validator
109123
(initial-schema-context->schema-validator context))

sitelib/text/json/schema/validators/core.scm

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@
9090
(schema-context:mark-dynamic-anchor! context value)
9191
(lambda (e ctx)
9292
(validator-context:set-dynamic-context! ctx context value)
93-
#t)
94-
)
93+
#t))
9594

9695
(define (($defs-handler name) value context schema-path)
9796
(define this-path (build-schema-path schema-path name))
@@ -105,8 +104,7 @@
105104
(assertion-violation 'json-schema:$defs
106105
"$defs must contain JSON object" value))
107106
(vector-for-each compile-definition value)
108-
#f
109-
)
107+
#f)
110108

111109
(define json-schema:definitions ($defs-handler "definitions"))
112110
(define json-schema:$defs ($defs-handler "$defs"))

test/tests/text/json/schema/validators.scm

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,85 @@
99
(test-error (json-schema->json-validator ""))
1010
(test-error (json-schema->json-validator #(("if" . ""))))
1111

12+
;; schemata from https://json-schema.org/blog/posts/dynamicref-and-generics
13+
(define list-of-t
14+
#(
15+
("$schema" . "https://json-schema.org/draft/2020-12/schema")
16+
("$id" . "https://json-schema.example/list-of-t")
17+
("type" . "array")
18+
("items" .
19+
#(
20+
("$dynamicRef" . "#T")
21+
))
22+
("$defs" .
23+
#(
24+
("content" .
25+
#(
26+
("$dynamicAnchor" . "T")
27+
("not" . #t)
28+
))
29+
)
30+
)
31+
))
32+
33+
(define list-of-string
34+
#(
35+
("$schema" . "https://json-schema.org/draft/2020-12/schema")
36+
("$id" . "https://json-schema.example/list-of-string")
37+
("$ref" . "list-of-t")
38+
("$defs" .
39+
#(
40+
("string-items" .
41+
#(
42+
("$dynamicAnchor" . "T")
43+
("type" . "string")
44+
)
45+
)
46+
)
47+
)
48+
))
49+
50+
(define list-of-int
51+
#(
52+
("$schema" . "https://json-schema.org/draft/2020-12/schema")
53+
("$id" . "https://json-schema.example/list-of-int")
54+
("$ref" . "list-of-t")
55+
("$defs" .
56+
#(
57+
("int-items" .
58+
#(
59+
("$dynamicAnchor" . "T")
60+
("type" . "integer")
61+
)
62+
)
63+
)
64+
)
65+
))
66+
67+
(define (test-validator validator . inputs)
68+
(for-each (lambda (expects&input)
69+
(let ((expects (car expects&input))
70+
(input (cadr expects&input)))
71+
(test-equal input expects (validate-json validator input))))
72+
inputs))
73+
74+
(test-validator (json-schema->json-validator list-of-string list-of-t)
75+
'(#t ("a" "b"))
76+
'(#f (null))
77+
'(#f (1 2))
78+
'(#f (#() ())))
79+
80+
(test-validator (json-schema->json-validator list-of-int list-of-t)
81+
'(#f ("a" "b"))
82+
'(#f (null))
83+
'(#t (1 2))
84+
'(#f (#() ())))
85+
86+
(test-validator (json-schema->json-validator
87+
list-of-string (json-schema->json-validator list-of-t))
88+
'(#t ("a" "b"))
89+
'(#f (null))
90+
'(#f (1 2))
91+
'(#f (#() ())))
1292

1393
(test-end)

0 commit comments

Comments
 (0)