JavaScript object properties validation. See below or src/_tests_/main-test.js for examples.
Install js-props-validator as NPM dependency.
npm install --save js-props-validator
import Props from 'js-props';
const validator = Props.object({
name: Props.string(),
age: Props.number(),
phone: Props.string().isOptional(),
country: Props.oneOf([ 'Austria', 'Germany', 'Switzerland' ]).withDefault('Germany')
});
validator.validate({
name: 'Egon',
age: 12
});
// => true
validator.validate({
name: 'Egon'
});
// => Will throw exception due to missing age
validator.validateValue({
name: 'Egon',
age: 'twelve'
});
// => false due to wrong type for age
validator.valueOrDefault({
name: 'Egon',
});
// => { name: 'Egon', country: 'Germany' }
Props.any([ ofType: Function | Array [, isOptional: boolean = false [, defaultValue: Any ]]])
Checks for any type. If ofType
is a function it will be used to validate the value. E.g.:
const validator = Props.any((value) => value > 10);
validator.validateValue(13) === true;
validator.validateValue(9) === false;
If ofType
is an array of Type
the value will be checked if it is one of the given types. E.g.:
const validator = Props.any([ Props.number(), Props.string() ]);
validator.validateValue(12) === true;
validator.validateValue(twelve) === true;
validator.validateValue([ 12 ]) === false;
Props.array([ ofType: Type [, isOptional: boolean = false [, defaultValue: Array ]]])
Checks for arrays. ofType
can be defined as Type. E.g.:
const validator = Props.array(Props.string());
validator.validateValue([ "a", "b", "c" ]) === true;
validator.validateValue([ 1, 2, 3 ]) === false;
Props.bool([ isOptional: boolean = false [, defaultValue: boolean ]])
Checks for boolean.
Props.oneOf(values: Array [, isOptional: boolean = false [, defaultValue: Any ]])
Checks for a set of defined values. E.g.:
const validator = Props.oneOf([ 1, 'A', 2 ]);
validator.validateValue(1) === true;
validator.validateValue('B') === false;
Props.func([ isOptional: boolean = false [, defaultValue: Function ]])
Checks for functions.
Props.number([ isOptional: boolean = false [, defaultValue: Number ]])
Checks for numbers.
Props.object(ofType: Object | Type [, isOptional: boolean = false [, defaultValue: Number ]])
ofType
can be a predefined object structure. E.g.:
const validator = Props.object({
name: Props.string(),
age: Props.number(),
phone: Props.string().isOptional(),
country: Props.oneOf([ 'Austria', 'Germany', 'Switzerland' ]).withDefault('Germany')
});
validator.validate({
name: 'Egon',
age: 12
}) === true;
If ofType
is a type, the validator checks if every property of the object meets the type's validations. E.g.:
const validator = Props.object(Props.object({
label: Props.string(),
value: Props.any([ Props.number(), Props.string() ])
}));
validator.validateValue({
id: {
label: "#",
value: 0
},
name: {
label: "Name",
value: "Egon"
}
}) === true;
validator.validateValue({
id: {
value: 0
},
name: {
label: "Name",
value: "Egon"
}
}) === false;
Props.string([ isOptional: boolean = false [, defaultValue: String ]])
Checks for strings.
Every type supports the following listed methods.
The validation will return true
if value is not set.
const validator = Props.number().isOptional(); // alternative to: Props.number(true);
validator.validateValue(1) === true;
validator.validateValue(undefined) === true;
validator.validateValue("one") === false;
The validation will return a default value when calling valueOrDefault
on a undefined value. Note: isOptional
will be automatically set to true
when calling withDefault
.
const validator = Props.number().withDefault(42); // alternative to: Props.number(true);
validator.validateValue(1) === true;
validator.validateValue(undefined) === true;
validator.validateValue("one") === false;
validator.valueOrDefault(1) === 1;
validator.valueOrDefault(undefined) === 42;
The Method will validate the value, if the validation fails an error will be thrown. valueName
is used within the error message if set.
const validator = Props.object({
name: Props.string(),
age: Props.number(),
phone: Props.string().isOptional(),
country: Props.oneOf([ 'Austria', 'Germany', 'Switzerland' ]).withDefault('Germany')
});
validator.validate({
name: 'Egon'
});
// => Will throw exception due to missing age
Like validate
but it will log an warning instead of throwing an error. Returns true
if validation was successful, otherwise false
.
Returns value
or the default value when set and value === undefined
. See example of withDefault
.