Skip to content

Commit

Permalink
Fixing remote ref tests on draft-7
Browse files Browse the repository at this point in the history
The late initialisation is already executed on the detached
context but the anchor / json pointer resolution requires late
initialisation. Adding ugly API layer to resolve this issue.
  • Loading branch information
ktakashi committed Nov 20, 2023
1 parent 5aa0b62 commit 914ff91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 8 additions & 3 deletions sitelib/text/json/schema/validators/api.scm
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
make-disjoint-context
make-schema-context

schema-context->schema-validator/late-initiation

schema-context:find-by-id schema-context:set-id!
schema-context:root-schema
schema-context:find-by-anchor schema-context:add-anchor!
Expand Down Expand Up @@ -518,10 +520,14 @@
validator)

(define (initial-schema-context->schema-validator initial-context)
(schema-context->schema-validator/late-initiation initial-context '("#")))

(define (schema-context->schema-validator/late-initiation context schema-path)
(define (finish validator)
(schema-context:execute-late-init! initial-context)
(schema-context:execute-late-init! context)
validator)
(finish (schema-context->schema-validator initial-context '("#"))))
(finish (schema-context->schema-validator context schema-path)))


;; schema-context -> validator
;; schema-path is debug or reporting purpose
Expand Down Expand Up @@ -569,7 +575,6 @@
(define (initializer)
(schema-validator->core-validator
(schema-context->schema-validator context schema-path)))

(cond ((hashtable-ref cache context #f))
(else
(let ((validator (schema-context:delayed-validator
Expand Down
17 changes: 13 additions & 4 deletions sitelib/text/json/schema/validators/ref.scm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

(define (ref-not-found value schema-path)
(assertion-violation 'json-schema:$ref "$ref not found" value schema-path))
(define (check-anchor schema id anchor schema-path)
(define (check-anchor schema id anchor schema-path remote?)
(cond ((not anchor)
(cond ((schema-context-validator schema))
;; in case of cross reference or self $id reference
Expand All @@ -79,6 +79,15 @@
(ref-not-found anchor schema-path))
(let ((schema (make-schema-context s schema)))
(cond ((schema-context-validator schema))
;; if it's from remote schema, then the target schema
;; is already resolved (i.e. after
;; initial-schema-context->schema-validator procedure
;; execution), which means no late execution will be
;; done. So, the delayed validator initializer won't
;; be executed by default.
(remote?
(schema-context->schema-validator/late-initiation
schema (list anchor)))
(else (->cached-validator schema id anchor))))))
((string-null? anchor)
;; recursive
Expand All @@ -100,7 +109,7 @@
;; If the schema has it, then it'd be overwritten anyway.
(json-schema:$id id this-context "#")
(let ((validator (initial-schema-context->schema-validator this-context)))
(cond ((check-anchor this-context id anchor schema-path))
(cond ((check-anchor this-context id anchor schema-path #t))
(else validator))))))

(define (find-by-id value context)
Expand Down Expand Up @@ -130,10 +139,10 @@
(cond ((not schema)
(resolve-external-schema context id
anchor schema-path))
((check-anchor schema #f anchor schema-path))
((check-anchor schema #f anchor schema-path #f))
(else (schema-context-validator schema))))))
(list (string-append (or id "") "#"))))
((check-anchor schema #f anchor schema-path))
((check-anchor schema #f anchor schema-path #f))
(else
(schema-context:delayed-validator context
(lambda ()
Expand Down

0 comments on commit 914ff91

Please sign in to comment.