|
156 | 156 |
|
157 | 157 |
|
158 | 158 |
|
159 |
| -(defn validate-string-format [{format "format"} data error ok |
| 159 | +(defn validate-string-format [{format "format" :as schema} data error ok |
160 | 160 | {lax-date-time-format? :lax-date-time-format?}]
|
161 | 161 | (when format
|
162 | 162 | (let [e (gensym "E")]
|
|
171 | 171 | "uri" 'webjure.json-schema.validator.format/validate-uri
|
172 | 172 | "email" 'webjure.json-schema.validator.format/validate-email
|
173 | 173 | (do
|
174 |
| - (println "WARNING: Unsupported format: " format) |
| 174 | + (println "WARNING: Unsupported format: " format " in schema: " schema) |
175 | 175 | `(constantly nil)))
|
176 | 176 | ~data))]
|
177 | 177 | ~(error e)
|
|
337 | 337 | additional-items "additionalItems" :as schema} data error ok options]
|
338 | 338 | (when item-schema
|
339 | 339 | (let [e (gensym "E")
|
| 340 | + c (gensym "C") |
340 | 341 | item (gensym "ITEM")
|
341 | 342 | item-error (gensym "ITEM-ERROR")]
|
342 | 343 | `(if-not (sequential? ~data)
|
|
370 | 371 |
|
371 | 372 | ;; Schema is an array, validate each index with its own schema
|
372 | 373 | (sequential? item-schema)
|
373 |
| - `(if-let [~e (or |
374 |
| - (when (< (count ~data) ~(count item-schema)) |
375 |
| - {:error :too-few-items |
376 |
| - :expected-item-count ~(count item-schema) |
377 |
| - :actual-item-count (count ~data) |
378 |
| - :data ~data}) |
379 |
| - |
380 |
| - ~@(for [i (range (count item-schema))] |
381 |
| - `(let [~item (nth ~data ~i)] |
382 |
| - ~(validate (nth item-schema i) item options))) |
383 |
| - |
384 |
| - ;; If additional items is false, don't allow more items |
385 |
| - ~@(when (false? additional-items) |
386 |
| - [`(when (> (count ~data) ~(count item-schema)) |
387 |
| - {:error :additional-items-not-allowed |
388 |
| - :additional-items (drop ~(count item-schema) ~data) |
389 |
| - :data ~data})]) |
390 |
| - |
391 |
| - ;; If additional items is a schema, check each against it |
392 |
| - ~@(when (map? additional-items) |
393 |
| - [`(some (fn [~item] |
394 |
| - (when-let [e# ~(validate additional-items item options)] |
395 |
| - {:error :additional-item-does-not-match-schema |
396 |
| - :item-error e# |
397 |
| - :data ~data})) |
398 |
| - (drop ~(count item-schema) ~data))]) |
399 |
| - )] |
400 |
| - ~(error e) |
401 |
| - ~(ok))))))) |
| 374 | + `(let [~c (count ~data)] |
| 375 | + (if-let [~e (or |
| 376 | + #_(when (< (count ~data) ~(count item-schema)) |
| 377 | + {:error :too-few-items |
| 378 | + :expected-item-count ~(count item-schema) |
| 379 | + :actual-item-count (count ~data) |
| 380 | + :data ~data}) |
| 381 | + |
| 382 | + ~@(for [i (range (count item-schema))] |
| 383 | + `(when (> ~c ~i) |
| 384 | + (let [~item (nth ~data ~i)] |
| 385 | + ~(validate (nth item-schema i) item options)))) |
| 386 | + |
| 387 | + ;; If additional items is false, don't allow more items |
| 388 | + ~@(when (false? additional-items) |
| 389 | + [`(when (> ~c ~(count item-schema)) |
| 390 | + {:error :additional-items-not-allowed |
| 391 | + :additional-items (drop ~(count item-schema) ~data) |
| 392 | + :data ~data})]) |
| 393 | + |
| 394 | + ;; If additional items is a schema, check each against it |
| 395 | + ~@(when (map? additional-items) |
| 396 | + [`(some (fn [~item] |
| 397 | + (when-let [e# ~(validate additional-items item options)] |
| 398 | + {:error :additional-item-does-not-match-schema |
| 399 | + :item-error e# |
| 400 | + :data ~data})) |
| 401 | + (drop ~(count item-schema) ~data))]) |
| 402 | + )] |
| 403 | + ~(error e) |
| 404 | + ~(ok)))))))) |
402 | 405 |
|
403 | 406 | (defn validate-array-item-count [{min-items "minItems" max-items "maxItems"} data error ok options]
|
404 | 407 | (when (or min-items max-items)
|
|
477 | 480 | [{any-of "anyOf"} data error ok options]
|
478 | 481 | (when-not (empty? any-of)
|
479 | 482 | (let [e (gensym "E")]
|
480 |
| - `(if-not (or |
481 |
| - ~@(for [schema any-of] |
482 |
| - `(nil? ~(validate schema data options)))) |
483 |
| - (let [~e {:error :does-not-match-any-of |
484 |
| - :any-of ~any-of |
485 |
| - :data ~data}] |
486 |
| - ~(error e)) |
487 |
| - ~(ok))))) |
| 483 | + `(let [errors# [~@(for [schema any-of] |
| 484 | + (validate schema data options))]] |
| 485 | + (if (some nil? errors#) |
| 486 | + ~(ok) |
| 487 | + (let [~e {:error :does-not-match-any-of |
| 488 | + :any-of ~any-of |
| 489 | + :data ~data |
| 490 | + :errors errors#}] |
| 491 | + ~(error e))))))) |
488 | 492 |
|
489 | 493 | (defn validate-one-of
|
490 | 494 | "Validate match against one (and only one) of the given schemas."
|
|
0 commit comments