Rule kinds, live
Each form below runs the real validonce package in this page: the rules on the right are plain x-validonce
data, and evaluate() checks them as you type. Nothing is sent anywhere. The exact semantics of each kind are in
Rule format v1.
required_when
A field is required when a condition holds. Pick pro and leave the company empty.
{
"version": 1,
"rules": [
{
"id": "Signup.required_when.company",
"kind": "required_when",
"field": "company",
"when": {
"eq": {
"field": "plan",
"value": "pro"
}
},
"message": {
"key": "rules.company_required"
}
}
]
}
compare
One field compared with another field or a fixed value. Set an end date before the start date.
{
"version": 1,
"rules": [
{
"id": "Contact.compare.availableTo",
"kind": "compare",
"left": {
"field": "availableTo"
},
"op": "ge",
"right": {
"field": "availableFrom"
},
"type": "date",
"message": {
"key": "rules.available_to_after_from"
}
}
]
}
at_least_one_of
At least one of the fields must be filled in. It starts failing: fill in either one.
{
"version": 1,
"rules": [
{
"id": "Contact.at_least_one_of.email",
"kind": "at_least_one_of",
"fields": [
"email",
"phone"
],
"message": {
"key": "rules.contact_email_or_phone"
}
}
]
}
exactly_one_of
Exactly one of the fields must be filled in: none fails, and so do both.
{
"version": 1,
"rules": [
{
"id": "Payment.exactly_one_of.cardToken",
"kind": "exactly_one_of",
"fields": [
"cardToken",
"iban"
],
"message": {
"key": "rules.pay_one_way"
}
}
]
}
all_or_none
Either every field is filled in or none is. Fill in only the street.
{
"version": 1,
"rules": [
{
"id": "Contact.all_or_none.street",
"kind": "all_or_none",
"fields": [
"street",
"city",
"postalCode"
],
"message": {
"key": "rules.address_all_or_none"
}
}
]
}
All four Contact rules together
These are the rules exactly as the six example backends export them (conformance/golden/). Change the country
to US with a partial address, or clear both email and phone, and several rules fail at once. The same form
runs against a real server in the playground.
{
"rules": [
{
"fields": [
"email",
"phone"
],
"id": "Contact.at_least_one_of.email",
"kind": "at_least_one_of",
"message": {
"key": "rules.contact_email_or_phone"
}
},
{
"fields": [
"street",
"city",
"postalCode"
],
"id": "Contact.all_or_none.street",
"kind": "all_or_none",
"message": {
"key": "rules.address_all_or_none"
}
},
{
"field": "postalCode",
"id": "Contact.required_when.postalCode",
"kind": "required_when",
"message": {
"key": "rules.postal_code_required_us"
},
"when": {
"eq": {
"field": "country",
"value": "US"
}
}
},
{
"id": "Contact.compare.availableTo",
"kind": "compare",
"left": {
"field": "availableTo"
},
"message": {
"key": "rules.available_to_after_from"
},
"op": "ge",
"right": {
"field": "availableFrom"
},
"type": "date"
}
],
"version": 1
}