- required - true / false Every object key defecation can be required, by default is false.
- type - For any validations need to provide type of validation, example: number, string etc..
{
"name": {
"type": "string",
"required": true
}
}
- All JS types are supported, if types are not listed bellow they have a regular validation of JS with '
typeof
' method.
All available properties for string values:
- minLength - represent the minimum length of the string value. [OPTIONAL]
- maxLength - represent the maximum length of the string value. [OPTIONAL]
- options - an array of options that the string can be. [OPTIONAL]
- isEmail - is the string is email? true / false. the email format is
someEmail@domain.co
. [OPTIONAL] - textAndNumbers - allow only text and number in the string, numbers from [1-9] and characters [a-z, A-Z]
{
"userType": {
"type": "string",
"minLength": 5,
"maxLength": 15,
"options": [
"type1",
"type2",
"type3"
],
"textAndNumbers": true
}
}
Means the "userType" property must be a string text and number ONLY, not less then 5 length, not higher the 15 length and ONLY one of the options 'type1', 'type2', 'type3'.
All available properties for number values:
- minNum - represent the minimum number of the number value.[OPTIONAL]
- maxNum - represent the maximum number of the number value.[OPTIONAL]
- numRange - represent range that the number can be in between. [OPTIONAL]
- options - an array of options that the number can be [OPTIONAL]
{
"age": {
"type": "number",
"minNum": 10,
"maxNum": 20,
"numRange": [10,20]
}
}
Means the "age" property can must be a number, not less then 10 not higher then 20 and between the range 10,20.
All available properties for ip values:
- options - an array of options that the ip can be [OPTIONAL]
{
"ip": {
"type": "ip",
"options": [
"127.0.0.1",
"0.0.0.0"
]
}
}
Means the "ip" property needs to be a valid ip and ONLY one of the options "127.0.0.1", "0.0.0.0".
All available properties for date values:
- format - represent the format that the date should be, reference to
moment.js
formats.[REQUIRED]
{
"today": {
"type": "date",
"format": "YYYY-MM-DD HH:mm:ss"
}
}
Means the "today" property can be only a date in format "YYYY-MM-DD HH:mm:ss".
All available properties for date values:
- objectSchema - Every object value can have a 'objectSchema' property to validate, in other word nested objects, the objectSchema accept all the validationObjects syntax.
{
"phone": {
"type": "object",
"required": true,
"objectSchema": {
"name": {
"type": "string",
"required": true
},
"number": {
"type": "number",
"required": true
}
}
}
}
Means the "phone" property must be defined and must be an object, the object of "phone" must contain the properties "name" and "number", "name" - must be defined and to be a string. "number" - must be defined and to be a number.
It's possible to validate nested object properties, just defined them properly.
All available properties for array values:
- validArraySchema - represent the types of values of the array.
- NOTE: if the values of the array is an objects, you must provide a "type": "object" and "objectSchema", look at the example bellow for reference.
{
"ips": {
"type": "array",
"required": true,
"validArraySchema": {
"type": "object",
"objectSchema": {
"name": {
"type": "string",
"minLength": 5,
"textAndNumbers": true,
"required": true
},
"ip": {
"type": "ip",
"required": true
}
}
}
}
}
Means the "ips" property must be defined and must be an array, the values of the array must be an objects, and every object must have "name" and "ip" properties. The "name" property must be defined, string, text and numbers only and minimum 5 length. The "ip" property must be defined and to be a valid ip address.
It's possible to validate nested objects inside the array and every value inside the array, just defined them properly.
Validators.email({require: true})
// Will return
{
"type": "string",
"isEmail": true,
"required": true
}
Validators.username({ required: true })
// Will return
{
"type": "string",
"minNum": 5,
"required": true,
"textAndNumbers": true
}
Validators.date({ required: true })
// Will return
{
"type": "date",
"format": "YYYY-MM-DD HH:mm:ss",
"required": false
}
Validators.id()
// Will return
{
"type": "number",
"minNum": 1,
"required": false
}
Validators.number({required: true, minNum: 1, maxNum: 10})
// Will return
{
"type": "number",
"required": true,
"minNum": 1,
"maxNum": 10
}
Validators.string({ required: true, minLength: 5 })
// Will return
{
"type": "string",
"required": true,
"minLength": 5
}
Validators.ip({ required: true, options: ['127.0.0.1', '127.0.1.1'] })
// Will return
{
"type": "ip",
"required": true,
"options": [
"127.0.0.1",
"127.0.1.1"
]
}
Validators.object({
required: true,
objectSchema: {
id: Validators.id({ required: true }),
name: Validators.string({ required: true })
}
})
// Will return
{
"type": "object",
"required": true,
"objectSchema": {
"id": {
"type": "number",
"minNum": 1,
"required": true
},
"name": {
"type": "string",
"required": true
}
}
}
Validators.array({
validArraySchema: {
type: 'object',
objectSchema: {
id: Validators.id({ required: true }),
name: Validators.string({ required: true })
}
}
})
// Will return
{
"type": "array",
"validArraySchema": {
"type": "object",
"objectSchema": {
"id": {
"type": "number",
"minNum": 1,
"required": true
},
"name": {
"type": "string",
"required": true
}
}
}
}
Validators.validateIp({ keyValue, objectKey, options });
Validators.validateArray({ type, keyValue, objectKey, validArraySchema });
Validators.validateDate({ objectKey, keyValue, type, format });
Validators.validateNumber({ keyValue, type, objectKey, minNum, maxNum, numRange, options });
Validators.validateString({ keyValue, type, objectKey, minLength, maxLength, options, isEmail, textAndNumbers });
Validators.validateJSTypes({ keyValue, objectKey, type });
Validators.validateObject({ objectKey, keyValue, type, objectSchema });
const validationTest = async () => {
const objectToValidate = {
id: 10,
email: 'ryan@utopia.com',
username: 'Ryan432',
date: '2020-10-15 18:05:00',
number: 5,
string: 'min length 4',
ip: '127.0.0.1',
object: {
id: 10,
name: 'object name'
},
array: [
{
id: 10,
name: 'array object'
}
]
};
const validObject = {
id: Validators.id(),
email: Validators.email({ required: true }),
username: Validators.username({ required: true }),
date: Validators.date(),
number: Validators.number({ required: true, minNum: 1, maxNum: 10 }),
string: Validators.string({ required: true, minLength: 5 }),
ip: Validators.ip({ required: true, options: ['127.0.0.1', '127.0.1.1'] }),
object: Validators.object({
required: true,
objectSchema: {
id: Validators.id({ required: true }),
name: Validators.string({ required: true })
}
}),
array: Validators.array({
validArraySchema: {
type: 'object',
objectSchema: {
id: Validators.id({ required: true }),
name: Validators.string({ required: true })
}
}
})
};
try {
const validationRes = await Validators.objectValidator(objectToValidate, validObject);
console.log(validationRes);
} catch (err) {
console.log(JSON.stringify({ err }, 2, 5));
}
};
validationTest();
Success Result - Means all the validations passed successfully, the object is valid based on the schema provided.
// validationRes
{
valid: true;
}
{
"err": {
"username": "system",
"extraDetails": {
//Inside the validationErrors you have very specific error about the failures.
"validationErrors": [
{
"valid": false,
"message": "The property 'email' is invalid email, got 'ryan@utopia'."
},
{
"valid": false,
"message": "The property 'ip' can be only one of the options [127.0.0.1,127.0.1.1]."
}
],
//The valid object schema.
"validObjectSchema": {
"id": {
"type": "number",
"minNum": 1,
"required": false
},
"email": {
"type": "string",
"isEmail": true,
"required": true
},
"username": {
"type": "string",
"minNum": 5,
"required": true,
"textAndNumbers": true
},
"date": {
"type": "date",
"format": "YYYY-MM-DD HH:mm:ss",
"required": false
},
"number": {
"type": "number",
"required": true,
"minNum": 1,
"maxNum": 10
},
"string": {
"type": "string",
"required": true,
"minLength": 5
},
"ip": {
"type": "ip",
"required": true,
"options": [
"127.0.0.1",
"127.0.1.1"
]
},
"object": {
"type": "object",
"required": true,
"objectSchema": {
"id": {
"type": "number",
"minNum": 1,
"required": true
},
"name": {
"type": "string",
"required": true
}
}
},
"array": {
"type": "array",
"validArraySchema": {
"type": "object",
"objectSchema": {
"id": {
"type": "number",
"minNum": 1,
"required": true
},
"name": {
"type": "string",
"required": true
}
}
}
}
}
},
"stackTrace": "Error\n at Function.objectValidator (file:///_dirname/backend/src/shared/Validators.js:426:10)"
}
}