vldt
v0.2.0
Published
Object validation made easy
Readme
vldt
Object validation made easy.
Defining Fields
To define a field use a string formatted like so:
[<prefix>]<type>[<array_indicator>][<min>][<max>]Then use this string to create a field:
let field = Field.create(<definition>)For instance:
let field = Field.create("*int>10")Prefixes
*: required. Required fields need to exist and contain a valid other thannullorundefined.-: hidden. Hidden fields are not projected (see projection).!: unique. Unique fields are not checked by VLDT itself. Use the field'sisUniqueproperty when saving data to a database for instance.
Types
stringnumber- accepts both, integers and floatsint- accepts only integers (i.e.1.3would not be accepted)float- accepts floats (basically the same asnumber. When projected values are forced into float format however)bool-true,false, and all truthy values (https://developer.mozilla.org/en-US/docs/Glossary/Truthy)email
Array Indicator
If a field is supposed to be an array, use the array indicator []. For instance:
'string[]'Min / Max
To require a minimum / maximum value, use > and <. For instance:
int>1- the value needs to be greater than1.int<10- the value needs to be smaller than10int>1<10- the value needs to be greater than1and smaller than10
Notes:
- If the type is
string, the length of the string is checked. - If the type is
bool, min and max are ignored - If the type is
email, min and max are ignored - When dealing with an array field, the min and max are used to check the values in the array (not the length of the array. Array length validation will implemented at a later point)
Validation
After creating a field, use its validate(<value>) method:
let result = field.validate(8)The validation result is an object with two properties:
isValid- a bool set totrueif validation was successful, or set tofalseif noterrors- an array containing an object for each validation error. Each of these objects has two properties:message: a string containing a descriptive text of the error encounteredreason: a string expressing in one word the reason for the error. The following values are used:required: a value was required but not giventype: the value was of the wrong typeformat: the value was not formatted correctly (e.g. a wrongly formatted e-mail address)
Roadmap
- Array length validation
- Embedded validation
