CX · Reference guidevalidate
PlaygroundDownloadsAboutv0.18.0-pre.1

validate

Validate a CX data record against a record-schema at runtime, in the JSON-Schema / pydantic style. validate-shape checks a map, element, or scalar against an inline [schema [field …] …], returning [ok $value] on success or an inspectable [invalid [violation …] …] report on failure — failure is data the caller iterates, never a thrown error. The vocabulary covers scalar constraints (type, pattern, min / max, min-length / max-length, required / optional) plus the structural child elements [enum …], nested [schema …], [extends $Base] and the keywords strict= / validate-with=, with patterns matched under RE2. This is the data-record sibling of the document-tree validator in spec/03-approved/core/schema.md, sharing one scalar-constraint vocabulary.

validate:validate-shape

[$validate:validate-shape] -> element — Validate a value against an inline schema, returning [ok $value] or [invalid …].

      [?lib 'cx-stdlib/validate']
[$validate:is-ok
  [$validate:validate-shape
    [user [email "alice@example.com"] [score 87]]
    [schema
      [field name="email" type="string" pattern=".+@.+\\..+"]
      [field name="score" type="int" min=0 max=100]]]]
    
      true
    

validate:validate-against

[$validate:validate-against] -> element — Validate a value against a registered named schema — name resolves to a content-hash, the hash to content, fail-closed.

      [?lib 'cx-stdlib/validate']
[$validate:validate-against
  [rec [email "a@b.co"]]
  "LEAD_SCHEMA"]
    
      cx-err:CXER1600
    

validate:register-schema

[$validate:register-schema] -> string — Bind a local name to a schema's content-hash and retain its canonical text (a hint-binding; returns the hash receipt).

      [?lib 'cx-stdlib/validate']
[?let [= $h [$validate:register-schema "Lead"
              [schema [field name="email" type="string" required=true]]]]
  [$validate:is-ok [$validate:validate-against [rec [email "a@b.co"]] "Lead"]]]
    
      true
    

validate:enum-values

[$validate:enum-values] -> sequence — Return a schema type's declared [enum …] members in DECLARED order, kind-faithful — atoms come back as atoms.

      [?lib 'cx-stdlib/validate']
[?let [= $h [$validate:register-schema 'Jobs' "[schema of=job]
[type status::atom [enum :ok :err :pending]]"]]
  [$validate:enum-values 'Jobs' 'status']]
    
      (:ok, :err, :pending)
    

validate:is-ok

[$validate:is-ok] -> bool — Report whether a validation result is [ok …] rather than [invalid …].

      [?lib 'cx-stdlib/validate']
[$validate:is-ok
  [$validate:validate-shape
    [user [email "alice@example.com"] [score 87]]
    [schema
      [field name="email" type="string" pattern=".+@.+\\..+"]
      [field name="score" type="int" min=0 max=100]]]]
    
      true
    

validate:errors-of

[$validate:errors-of] -> [sequence element] — Return the sequence of [violation …] children of a result, empty for [ok …].

      [?lib 'cx-stdlib/validate']
[$count
  [$validate:errors-of
    [$validate:validate-shape
      [rec [email "bad"] [score "abc"]]
      [schema
        [field name="email" type="string" pattern=".+@.+\\..+"]
        [field name="score" type="int"]]]]]
    
      2
    

validate:violation-paths

[$validate:violation-paths] -> [sequence string] — Project the CXPath path= of each violation in a result.

      [?lib 'cx-stdlib/validate']
[$validate:violation-paths
  [$validate:validate-shape
    [rec [address [zip "abc"]]]
    [schema [field name="address" type="element" [schema
      [field name="zip" type="string" pattern="^\\d{5}(-\\d{4})?$"]]]]]]
    
      ('/address/zip')
    

validate:violation-messages

[$validate:violation-messages] -> [sequence string] — Project the human-readable message= of each violation in a result.

      [?lib 'cx-stdlib/validate']
[$validate:violation-messages
  [invalid [violation code="TYPE_MISMATCH" path="/score"
                  message="score: expected int, got \"abc\""]]]
    
      ('score: expected int, got "abc"')