Developer Tools

vvalidate

Checks maps against a schema of types, required fields and ranges.

Released v1.0.0 · 2026-09-28
Import
use "stdlib/validate.ven"

So a bad value is caught at the door.

vvalidate.ven
fields = new_map()
map_set(fields, "name", field_spec("Text", yes, new_map()))
print schema(fields)
Reference (7 functions)

API reference

merged_map(base, extra)

Merges extra over base into one new Map, with extra winning on key clashes.

base = new_map()
map_set(base, "a", 1)
map_set(base, "b", 2)
extra = new_map()
map_set(extra, "b", 3)
map_set(extra, "c", 4)
print merged_map(base, extra)
:: prints: {"a": 1, "b": 3, "c": 4}

field_spec(type_name, required, constraints)

Builds one field spec Map: a "type" and "required" flag plus any extra constraints.

constraints = new_map()
map_set(constraints, "min", 0)
map_set(constraints, "max", 120)
print field_spec("Integer", yes, constraints)
:: prints: {"type": "Integer", "required": yes, "min": 0, "max": 120}

schema(field_specs)

Builds a schema from a Map of field name to field spec.

fields = new_map()
map_set(fields, "name", field_spec("Text", yes, new_map()))
print schema(fields)
:: prints: {"name": {"type": "Text", "required": yes}}

violations_of(s, data)

Checks data against schema s, returning a list of human-readable violation messages.

constraints = new_map()
map_set(constraints, "min", 0)
map_set(constraints, "max", 120)
name_constraints = new_map()
map_set(name_constraints, "min_length", 1)
fields = new_map()
map_set(fields, "age", field_spec("Integer", yes, constraints))
map_set(fields, "name", field_spec("Text", yes, name_constraints))
s = schema(fields)
bad_data = new_map()
map_set(bad_data, "age", 200)
map_set(bad_data, "name", "")
print violations_of(s, bad_data)
:: prints: ["'age' must be <= 120.", "'name' must have length >= 1."]

validate(s, data)

Validates data against schema s, raising when any violation is found.

constraints = new_map()
map_set(constraints, "min", 0)
map_set(constraints, "max", 120)
fields = new_map()
map_set(fields, "age", field_spec("Integer", yes, constraints))
s = schema(fields)
bad_data = new_map()
map_set(bad_data, "age", 200)
attempt
    validate(s, bad_data)
rescue e
    print "raised: " + as_text(e)
:: prints: raised: 'age' must be <= 120.

is_valid(s, data)

Tests whether data satisfies every field in schema s, without raising.

constraints = new_map()
map_set(constraints, "min", 0)
map_set(constraints, "max", 120)
fields = new_map()
map_set(fields, "age", field_spec("Integer", yes, constraints))
s = schema(fields)
good_data = new_map()
map_set(good_data, "age", 30)
bad_age = new_map()
map_set(bad_age, "age", -1)
print is_valid(s, good_data)
print is_valid(s, bad_age)
:: prints: yes
::         no

coerce(s, data)

Copies data and converts Text values to their schema's declared numeric type.

fields = new_map()
map_set(fields, "age", field_spec("Integer", yes, new_map()))
s = schema(fields)
coerce_data = new_map()
map_set(coerce_data, "age", "42")
map_set(coerce_data, "name", "Ada")
print coerce(s, coerce_data)
:: prints: {"age": 42, "name": "Ada"}

Get notified when vvalidate changes

Get notified about

New versions and security advisories.

New and updated packages, once a day.

News on the math model built in Venutian.