young-validator
a simple, functional schema validator for javscript.
Usage
import { any, array, boolean, dictionary, enumeration, number, object, or, string } from 'young-validator';
// string
const stringValidator = string({ minLength: 10 });
stringValidator('test string'); // OK
stringValidator('test'); // ERROR: value length must be at least 10
stringValidator(undefined); // ERROR: value must be defined
// number
const numberValidator = number({ optional: true, max: 1000, min: 0, strict: false });
numberValidator(23); // OK
numberValidator(undefined); // OK, because optional: true
numberValidator('44'); // OK, because strict: false
numberValidator(-10); // ERROR: value must be greater than 0
// boolean
const booleanValidator = boolean(); // config is always optional
booleanValidator(false); // OK
booleanValidator(''); // ERROR: value must be a boolean
// array
const arrayConfig = array({ maxLength: 5 });
// any validator can be passed into the second argument
const anyArrayValidator = arrayConfig(any());
const stringArrayValidator = arrayConfig(string());
const numberArrayValidator = arrayConfig(number());
const arrayArrayValidator = arrayConfig(anyArrayValidator);
anyArrayValidator(['string', 100, {}]); // OK
stringArrayValidator(['string', 100, {}]); // ERROR: element at index 1 must be a string
numberArrayValidator(['string', 100, {}]); // ERROR: element at index 0 must be a number
arrayArrayValidator(['string', 100, {}]); // ERROR: element at index 0 must be an array
anyArrayValidator([1, 2, 3, 4, 5, 6]); // ERROR: array length must be less than or equal to 5
arrayArrayValidator([[1, 2, 3], [1, 2, 3]]); // OK
// dictionary
const dictionaryConfig = dictionary();
// same as array, any validator can be passed into the second argument
const stringDictionaryValidator = dictionaryConfig(string());
const arrayDictionaryValidator = dictionaryConfig(array()(any()));
const anyDictionaryValidator = dictionaryConfig(any());
anyDictionaryValidator({}); // OK
stringDictionaryValidator({}); // OK
stringDictionaryValidator({ a: 'a' }); // OK
stringDictionaryValidator({ a: 1 }); // ERROR: value at property a must be a string
arrayDictionaryValidator({ a: [], b: {} }); // ERROR: value at property b must be an array
arrayDictionaryValidator({ a: [], b: [] }); // OK
// enum
// takes a typescript enum or equivelant object
enum Example {
One = 'one',
Two = 'two',
}
enum AnotherEnum {
Three = 'three',
Four = 'four',
}
const enumValidator = enumeration()(Example);
enumValidator(Example.One); // OK
enumValidator(AnotherEnum.Four); // ERROR: 'four' does not exist in ['one','two']
// object
// a dictionary of any validators can be passed into the second argument
const objectAValidator = object()({
propA: string({ optional: true }),
propB: number(),
});
const objectBValidator = object()({
propC: objectAValidator,
propD: any(),
});
objectAValidator({ propB: 123 }); // OK
objectAValidator({ propA: 'string', propB: 123 }); // OK
objectAValidator({ propA: 'string', propB: 123, propC: true }); // OK, use option trim:true to trim extra props. passes test though
objectAValidator({ propA: 'string' }); // ERROR: { propB: must be defined }
objectAValidator({ propA: false }); // ERROR: { propB: must be defined, propA: must be a string }
objectBValidator({ propC: { propB: 123 }, propD: true }); // OK
objectBValidator({ propC: {}, propD: true }); // ERROR: { propC: { propB: must be defined } }
// or
const stringOrNumberValidator = or(string(), number());
stringOrNumberValidator(123); // OK
stringOrNumberValidator('test'); // OK
stringOrNumberValidator(true); // ERROR
// ADVANCED
// these validators can be chained in any way you can think of to accomplish advanced scenarios
// inheritance
enum Gender {
Male,
Female,
Other,
}
const personSchema = {
name: string(),
age: number(),
gender: enumeration()(Gender),
};
const employeeValidator = object()({
...personSchema,
ssn: string(),
});
const parentValidator = object()({
...personSchema,
children: array()(object()(personSchema)),
});
// combination
const complexValidator = object()({
propA: array({ optional: true })(
object()({
propA_1: string(),
propA_2: string(),
propA_3: string(),
propA_4: object()({
propA_4_1: array()(
object()({
anotherProp: any(),
})
),
}),
})
),
propB: dictionary()(or(string(), number(), boolean())),
email: string({ match: /emailregexhere/ }),
});
Future Work
This is a really new project that i built out of necessity. I plan to expand to more types, add more options for existing types, and improve performance in the coming months.
The codebase is very simple, has no dependecies, and should be fairly easy to adapt to your specific needs.
also, in the near future i will add support for custom validators.