1.2.2 • Published 3 years ago
@sovgut/object-validation v1.2.2
import validateObject, { property, Scheme } from '@sovgut/object-validation';
const request = {
body: {
email: 'example@email.com',
password: 'superSecretPassw0rd!',
confirmPassword: 'superSecretPassw0rd!',
},
};
const validationScheme: Scheme[] = [
property('email:string'),
property('password:string').min(8).max(32),
property('confirmPassword:string').min(8).max(32).compareWith('password'),
property('age:number').optional(),
];
const { success, reason } = validateObject(request.body, validationScheme);
console.log(success, reason);
API
property(key: "name:primitive"): Scheme
Params
key
- first part of this string is the property name from the object, second is the type of property from the object
Returns
min(length: number)
- sets the minimum of length for string sourcemax(length: number)
- sets the maximum of length for string sourcecompareWith(key: string)
- sets comparison within source and target propertiesonValidate(callback: (value: any) => string | void)
- sets additional custom validation for source property, should return an error string like"something-wrong-happened"
or nothing (void
)optional()
- sets source property as optional and any validations just skipped this property if error raised
validateObject(target: any, scheme: Scheme[]): Response
Params
target
- the source object that should be validatedscheme
- validation scheme array
Returns
response
- the response object that havesuccess
andreason
properties,reason
isundefined
by default, this object have text when error raised