Skip to main content

Validation

Implementing server-side validation is very easy with Manifest.

You can use the built-in custom validators to ensure that the data you are receiving is correctly formatted.

Syntax

In your backend.yml, you can add a validation object that lists the properties and their validators:

entities:
Dog:
properties:
- name
- { name: age, type: number }
validation:
name: { minLength: 3 } # The name should have at least 3 characters.
age: { min: 1, max: 30 } # Age should be a number between 1 and 30.
Tip

Type validation is natively implemented with property types. You do not need to set it.

Ex: sending a string value for a boolean property type will throw an error.

Inline syntax

If you want to keep it short you can simply pass the validation object to the property object:

entities:
🧑🏽‍🦱 Author:
properties:
- name
- { name: email, type: email, validation: { isNotEmpty: true } }
- { name: bio, type: textarea, validation: { maxLength: 300 } }

Both syntaxes (block and inline) can be used simultaneously. In case of declaration conflict, the inline declaration will prevail.

Optional values validation

If you have some values that are optional but that need to be validated if present, you can add the isOptional validator. The value will be compared against validators only if not undefined or null. Example:

entities:
Employee:
properties:
- name
- {
name: email,
type: email,
validation: { contains: '@company.com', isOptional: true }
} # If provided, the email should contain "@company.com"

Response

If the validation fails, the response will list the validation error(s) in its body:

// Create a new Employee
POST /api/dynamic/employees
Content-Type: application/json
{
"name": "John Doe",
"email": "john@manifest.build"
}

// Response.
[
{
"property": "email",
"constraints": {
"contains": "The value must contain @company.com"
}
}
]

Available validators

ValidatorTypeDescription
requiredbooleanIndicates whether the property must not be empty.
isDefinedbooleanChecks if value is defined (!== undefined, !== null).
isOptionalbooleanChecks if given value is empty (=== null, === undefined) and ignores all validators.
equalsanyChecks if value equals ("===") comparison.
notEqualsanyChecks if value not equal ("!==") comparison.
isEmptybooleanIndicates whether the property can be empty.
isNotEmptybooleanIndicates whether the property must not be empty. Alias for required
isInarrayChecks if value is in an array of allowed values.
isNotInarrayChecks if value not in an array of disallowed values.
minnumberThe minimum value or length allowed for the property.
maxnumberThe maximum value or length allowed for the property.
containsstringChecks if string contains the seed.
notContainsstringChecks if string does not contain the seed.
isAlphabooleanChecks if the string contains only letters (a-zA-Z).
isAlphanumericbooleanChecks if the string contains only letters and numbers.
isAsciibooleanChecks if the string contains ASCII chars only.
isEmailbooleanChecks if the string is an email.
isJSONbooleanChecks if the string is valid JSON.
minLengthnumberChecks if the string's length is not less than given number.
maxLengthnumberChecks if the string's length is not more than given number.
matchesstringChecks if string matches the pattern.
isMimeTypebooleanChecks if the string matches to a valid MIME type format.