-
-
Notifications
You must be signed in to change notification settings - Fork 15
Date
Asserts if the entry is before a certain date
Signature:
before(date: string | number | Date, message: string)
Example:
Nope.date().before("2019-01-01").validate("2018-31-12"); // returns undefined
Nope.date().before("2019-01-01").validate("2019-01-02"); // returns the error message
Asserts if the entry is after a certain date
Signature:
after(date: string | number | Date, message: string)
Example:
Nope.date().after("2019-01-01").validate("2018-02-01"); // returns undefined
Nope.date().after("2019-01-01").validate("2018-31-12"); // returns the error message
Conditional validation of a key
Signature:
when(key: string | string[], conditionObject: { is: boolean | ((...args: any) => boolean), then: NopeSchema, otherwise: NopeSchema })
key
- set of keys (or a single key) that the is
predicate should run on. Note that you can access the parent object(s) by using the ../ syntax as shown in the 2nd example
conditionObject
:
is
- a boolean flag (which will run the .every method on the values and assert the against the passed is
) or a predicate that will decide what schema will be active at that moment.
then
- schema in case is
param is truthy
otherwise
- schema in case the is
param is falsy
Example:
const schema = Nope.object().shape({
check: Nope.boolean().required(),
test: Nope.string().when("check", {
is: true,
then: Nope.string().atLeast(5, "minError").required(),
otherwise: Nope.string().atMost(5).required(),
}),
});
schema.validate({
check: true,
test: "test",
}); // { test: 'minError' }
// or as a predicate
const schema2 = Nope.object().shape({
check: Nope.boolean(),
check2: Nope.boolean(),
test: Nope.string().when(["check", "check2"], {
is: (check, check2) => check && check2,
then: Nope.string().atLeast(5, "minError").required(),
otherwise: Nope.string().atMost(5).required(),
}),
});
schema.validate({
check: true,
check2: false,
test: "testing",
}); // { test: 'maxError' }
const schema = Nope.object().shape({
shouldCreateUser: Nope.boolean().required("reqbool"),
user: Nope.object().shape({
name: Nope.string().when("../shouldCreateUser", {
is: (str) => !!str,
then: Nope.string().required("required"),
otherwise: Nope.string().notAllowed("not allowed"),
}),
}),
});
const validInput1 = {
shouldCreateUser: true,
user: {
name: "user name",
},
};
const invalidInput1 = {
shouldCreateUser: true,
user: {
name: undefined,
},
};
expect(schema.validate(validInput1)).toEqual(undefined);
expect(schema.validate(invalidInput1)).toEqual({
user: {
name: "required",
},
});
Asserts if the entry is one of the defined options
Signature:
oneOf(options: string | ref[], message: string)
Example
Nope.string().oneOf(["a", "b", "c"]).validate("b"); // returns undefined
Nope.string().oneOf(["a", "b", "c"]).validate("d"); // returns the error message
Asserts if the entry is none of the defined options
Signature:
notOneOf(options: number | ref[], message: string)
Example:
Nope.string().notOneOf([1, 2, 3]).validate(5); // returns undefined
Nope.string().notOneOf([1, 2, 3]).validate(2); // returns the error message
Asserts if the entry is not nil (undefined or null)
Signature:
required(message: string)
Example:
Nope.string().required().validate("b"); // returns undefined
Nope.string().required().validate(); // returns the error message
Asserts if the entry is nil
Signature:
notAllowed(message: string)
Example:
Nope.string().notAllowed().validate(null); // returns undefined
Nope.string().notAllowed().validate("42"); // returns the error message
Add a custom rule
Signature:
test(rule: (entry: T) => string | undefined)
Example:
Nope.string()
.test((a) => (a === "42" ? undefined : "Must be 42"))
.validate("42"); // returns undefined
Nope.string()
.test((a) => (a === "42" ? undefined : "Must be 42"))
.validate("41"); // returns the error message
Runs the rule chain against an entry
Signature:
validate(entry: string | undefined | null)
Can be seen in use in the examples above