Skip to main content

schema

Sub-namespaces

Jump to function (24)

schema/coerce#

(coerce schema value)

Walks schema and coerces string-shaped input into schema-required types. Idempotent for already-typed values.

Example:

(coerce :int "42") ; => 42

schema/conform#

(conform schema value)

Coerces value against schema. Returns the coerced value on success, otherwise :phel.schema/invalid.

Example:

(conform :int "42") ; => 42

schema/deref-ref#

(deref-ref name)

Returns the schema registered under name, or nil.

Example:

(deref-ref :email)

schema/explain#

(explain schema value)

Returns nil when value conforms to schema. On mismatch returns {:schema schema :value value :errors [...]} with one error per violation, each carrying :path, :in, :schema, :value, :type.

Example:

(explain :int :oops)

schema/generate#

(generate schema)
(generate schema opts)

Generates a single value conforming to schema. Accepts :size and :seed options.

Example:

(generate :int)

schema/human-readable-explain#

(human-readable-explain result)

Renders an explain result as a multi-line human string. Returns nil for a nil input.

Example:

(human-readable-explain (explain :int :oops))

schema/instrument!#

(instrument! name f schema)

Registers f wrapped with schema under name. Returns the wrapped function. The original is preserved so unstrument! can restore it.

Example:

(instrument! :add add [:=> [:int :int] :int])

schema/instrumented?#

(instrumented? name)

Returns true if name is currently instrumented.

Example:

(instrumented? :add)

schema/invalid-marker#

Sentinel returned by conform when a value cannot be made to fit a schema.

schema/register!#

(register! name schema)

Registers schema under name in the global schema registry.

Example:

(register! :email [:and :string [:re "/@/"]])

schema/registered?#

(registered? name)

Returns true if a schema is registered under name.

Example:

(registered? :email)

schema/schema->gen#

(schema->gen schema)

Returns the phel\test\gen generator associated with schema.

Example:

(schema->gen :int)

schema/schema-args#

(schema-args schema)

Returns the positional arguments of schema (children past the head and optional options map).

Example:

(schema-args [:vector :int]) ; => [:int]

schema/schema-check?#

(schema-check?)

Returns true when runtime validation performed by the instrument helpers is currently enabled.

Example:

(schema-check?) ; => true

schema/schema-head#

(schema-head schema)

Returns the dispatch head (kind keyword) of schema.

Example:

(schema-head [:vector :int]) ; => :vector

schema/schema-options#

(schema-options schema)

Returns the options map of schema or {}.

Example:

(schema-options [:map {:closed true} [:k :int]]) ; => {:closed true}

schema/schema?#

(schema? x)

Returns true if x has the shape of a schema value.

Example:

(schema? :int) ; => true

schema/set-schema-check!#

(set-schema-check! enabled?)

Enables (true) or disables (false) runtime validation. Returns the new value.

Example:

(set-schema-check! false)

schema/unregister!#

(unregister! name)

Removes the schema bound to name. Returns nil.

Example:

(unregister! :email)

schema/unstrument!#

(unstrument! name)

Removes the instrumentation registered under name and returns the original, unwrapped function (or nil if no entry).

Example:

(unstrument! :add)

schema/validate#

(validate schema value)

Returns true if value conforms to schema, otherwise false.

Example:

(validate :int 1) ; => true

schema/with-schema-check#

(with-schema-check enabled? f)

Invokes zero-arg thunk f with runtime validation forced to enabled?. The previous value is restored on return or exception.

Example:

(with-schema-check false (fn [] (risky-fn)))

schema/wrap-with-function-schema#

(wrap-with-function-schema f schema)

Wraps f with the [:=> args ret] function schema.

Example:

(wrap-with-function-schema add [:=> [:int :int] :int])

schema/wrap-with-schema#

(wrap-with-schema f arg-schema return-schema)

Wraps f so calls validate arguments and return values against the supplied schemas.

Example:

(wrap-with-schema add [:int :int] :int)