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/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, referenced by string.

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

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"')