sema-validate 0.1.0

Schema validation for maps and data structures

API reference 13 exports

Exported declarations from this release. Parameter names and descriptions come directly from its source.

validate/all-of

Source :75
(validate/all-of . preds)
Value must satisfy every given predicate or validator.
View source definition
(define (validate/all-of . preds)
  "Value must satisfy every given predicate or validator."
  (hash-map
    :check   (fn (v) (every (fn (p) ((check-fn-of p) v)) preds))
    :message (string-append "expected all-of: "
                            (string/join (map message-of preds) ", "))
    :required? #t
    :nested  nil))

validate/assert!

Source :189
(validate/assert! sch data)
Return data if valid; raise an error listing every failure if not.
View source definition
(define (validate/assert! sch data)
  "Return data if valid; raise an error listing every failure if not."
  (let ((errs (validate/errors sch data)))
    (if (null? errs)
      data
      (error (string-append
               "validate/assert!: validation failed — "
               (string/join (map format-error errs) "; ")))))))

validate/errors

Source :173
(validate/errors sch data)
Return a list of error maps ({:path :message :value}). Empty list if valid.
View source definition
(define (validate/errors sch data)
  "Return a list of error maps ({:path :message :value}). Empty list if valid."
  (collect-errors (get sch :spec) data '()))

validate/in-range

Source :100
(validate/in-range lo hi)
Value must be a number within the inclusive range [lo, hi].
View source definition
(define (validate/in-range lo hi)
  "Value must be a number within the inclusive range [lo, hi]."
  (hash-map
    :check   (fn (v) (and (number? v) (>= v lo) (<= v hi)))
    :message (format "expected in-range ~a ~a" lo hi)
    :required? #t
    :nested  nil))

validate/list-of

Source :84
(validate/list-of pred)
Value must be a list whose every element matches the predicate or validator.
View source definition
(define (validate/list-of pred)
  "Value must be a list whose every element matches the predicate or validator."
  (hash-map
    :check   (fn (v) (and (list? v) (every (check-fn-of pred) v)))
    :message (string-append "expected list-of: " (message-of pred))
    :required? #t
    :nested  nil))

validate/matches

Source :92
(validate/matches pattern)
Value must be a string matching the given regex pattern.
View source definition
(define (validate/matches pattern)
  "Value must be a string matching the given regex pattern."
  (hash-map
    :check   (fn (v) (and (string? v) (regex/match? pattern v)))
    :message (string-append "expected string matching " (str pattern))
    :required? #t
    :nested  nil))

validate/max-length

Source :118
(validate/max-length n)
Value must be a string or list with at most n characters/elements.
View source definition
(define (validate/max-length n)
  "Value must be a string or list with at most n characters/elements."
  (hash-map
    :check   (fn (v) (cond ((string? v) (<= (string-length v) n))
                           ((list? v)   (<= (length v) n))
                           (#t #f)))
    :message (format "expected max-length ~a" n)
    :required? #t
    :nested  nil))

validate/min-length

Source :108
(validate/min-length n)
Value must be a string or list with at least n characters/elements.
View source definition
(define (validate/min-length n)
  "Value must be a string or list with at least n characters/elements."
  (hash-map
    :check   (fn (v) (cond ((string? v) (>= (string-length v) n))
                           ((list? v)   (>= (length v) n))
                           (#t #f)))
    :message (format "expected min-length ~a" n)
    :required? #t
    :nested  nil))

validate/one-of

Source :67
(validate/one-of . values)
Value must equal one of the given values.
View source definition
(define (validate/one-of . values)
  "Value must equal one of the given values."
  (hash-map
    :check   (fn (v) (any #(equal? % v) values))
    :message (string-append "expected one-of: " (string/join (map str values) ", "))
    :required? #t
    :nested  nil))

validate/optional

Source :58
(validate/optional pred)
Mark a field as optional: absent is fine, present values must still match. pred is a predicate, validator, or nested spec map.
View source definition
(define (validate/optional pred)
  "Mark a field as optional: absent is fine, present values must still match.
   pred is a predicate, validator, or nested spec map."
  (cond
    ((validator? pred) (assoc pred :required? #f))
    ((map? pred) (hash-map :check nil :message nil :required? #f :nested pred))
    (#t (hash-map :check pred :message (string-append "expected " (pred-name pred))
                  :required? #f :nested nil))))

validate/required

Source :50
(validate/required pred)
Mark a field as required. pred is a predicate, validator, or nested spec map.
View source definition
(define (validate/required pred)
  "Mark a field as required. pred is a predicate, validator, or nested spec map."
  (cond
    ((validator? pred) (assoc pred :required? #t))
    ((map? pred) (hash-map :check nil :message nil :required? #t :nested pred))
    (#t (hash-map :check pred :message (string-append "expected " (pred-name pred))
                  :required? #t :nested nil))))

validate/schema

Source :143
(validate/schema spec)
Create a schema from a spec map.
View source definition
(define (validate/schema spec)
  "Create a schema from a spec map."
  (hash-map :spec spec))

validate/valid?

Source :177
(validate/valid? sch data)
Return #t if data matches the schema, #f otherwise.
View source definition
(define (validate/valid? sch data)
  "Return #t if data matches the schema, #f otherwise."
  (null? (validate/errors sch data)))
About this reference

Generated with sema-reader 1.37.0 without executing package code. These are source declarations; dynamic exports, aliases, and runtime changes are not inferred.

Archive SHA-256 04c17fe46caa3ae4e57f134148722a2312477f78c7735b1cb7731c7e2ab938ef