-
-
Notifications
You must be signed in to change notification settings - Fork 15
Array
Required field (not nil)
Signature:
required<T>(message: string)
Example:
const validator = Nope.array().required("requiredError");
validator.validate(null); // returns requiredError
validator.validate([42]); // returns undefined
Require the values to be of a certain type
Signature:
of(type: Validatable<T>)
Example:
const schema = Nope.object().shape({
names: Nope.array().of(Nope.string().min(5)).required(),
});
schema.validate({
names: ["Geralt"],
}); // returns undefined;
If you are using async schema validation, please use ofAsync
instead of the normal of
Validate if array is greater than a certain length
Signature:
minLength(length: number, message?: string)
Example:
const validator = Nope.array().minLength(5, "minLengthErrorMessage");
validator.validate(undefined); // returns undefined
validator.validate([1, 2, 3, 4, 5, 6]); // returns undefined
validator.validate([1, 2, 3, 4]); // returns 'minLengthErrorMessage'
Max length of the field
Signature:
maxLength(length: number, message?: string)
Example:
const validator = Nope.array().maxLength(5, "maxLengthErrorMessage");
validator.validate(undefined); // returns undefined
validator.validate([1, 2, 3, 4]); // returns undefined
validator.validate([1, 2, 3, 4, 5, 6]); // returns 'minLengthErrorMessage'
Must contain a certain item
Signature:
mustContain(value: T, message?: string)
Example:
const validator = Nope.array().mustContain(2, "containError");
validator.validate([1, 3, 4]); // returns 'containError'
validator.validate([1, 2, 3, 4]); // returns undefined
Must only contain certain items
Signature:
hasOnly(values: T[], message?: string)
Example:
const validator = Nope.array().hasOnly([1, 2, 3], "invalidEntries");
validator.validate([1, 2, 3]); // returns undefined
validator.validate([1, 2, 3, 4]); // returns 'invalidEntries'
validator.validate([4, 5, 6]); // returns 'invalidEntries'
const validator = Nope.array()
.of(Nope.array().of(Nope.string()))
.hasOnly([["a"]], "invalidEntries");
validator.validate([["a"]]); // returns undefined
validator.validate([["b"]]); // returns 'invalidEntries'
Every item passes predicate. Alike Array.prototype.every
Signature:
every(predicate: item => boolean, message?: string)
Example:
const isEven = (value) => value % 2 === 0;
const validator = Nope.array().every(isEven, "everyError");
validator.validate(undefined); // returns undefined
validator.validate([]); // returns undefined
validator.validate([2, 4, 6]); // returns undefined
validator.validate([1, 2, 4]); // returns 'everyError'
validator.validate([1, 3, 5]); // returns 'everyError'
Some items pass the predicate. Alike Array.prorotype.some
Signature:
some(predicate: item => boolean, message?: string)
Example:
const isEven = (value: number) => value % 2 === 0;
const validator = Nope.array<any>().some(isEven, 'whereSomeError');
validator.validate(undefined) // returns undefined
validator.validate([]) // returns undefined
validator.validate([1,2,3]) // returns undefined
validator.validate([2,4,6]) // returns undefined
validator.validate([1,3,5]) // returns 'whereSomeError'