-
-
Notifications
You must be signed in to change notification settings - Fork 15
String
- regex
- url
- trim
- min
- greaterThan
- max
- lessThan
- exactLength
- between
- when
- oneOf
- notOneOf
- required
- notAllowed
- test
- validate
Asserts if the entry matches the pattern
Signature:
regex(regex: RegExp, message: string)
Example:
Nope.string().regex(/abc/i).validate("abc"); // returns undefined
Nope.string().regex(/abc/i).validate("123"); // returns the error message
Asserts if the entry is a valid URL
Signature:
url(message: string)
Example:
Nope.string().url().validate("http://google.com"); // returns undefined
Nope.string().url().validate("http:google.com"); // returns the error message
Asserts if the entry is a valid email
Signature:
email(message: string)
Example:
Nope.string().email().validate("[email protected]"); // returns undefined
Nope.string().email().validate("testgmail.com"); // returns the error message
It should remove the string provided (entry) without whitespace
Signature:
trim()
Example:
Nope.string().trim().email().validate(' [email protected] '); // returns the default string
Nope.string().email().validate(' [email protected] '); // returns the error message
alias for greaterThan
Signature:
min(length: number, message: string)
Asserts if the entry is smaller than a threshold
Signature:
greaterThan(length: number, message: string)
Example:
Nope.string().greaterThan(4).validate("https"); // returns undefined
Nope.string().greaterThan(4).validate("http"); // returns the error message
alias for lessThan
Signature:
max(length: number, message: string)
Asserts if the entry is greater than a threshold
Signature:
lessThan(length: number, message: string)
Example:
Nope.string().lessThan(4).validate("url"); // returns undefined
Nope.string().lessThan(4).validate("http"); // returns the error message
Asserts if the entry is of exact length
Signature:
exactLength(length: number, message: string)
Example:
Nope.string().exactLength(4).validate("test"); // returns undefined
Nope.string().exactLength(4).validate("testing"); // returns the error message
Asserts if the entry's length is between a range
Signature:
public between(startLength: number, endLength: number, atLeastMessage: String, atMostMessage: String)
Example:
Nope.string().between(3, 5).validate('test') // returns undefined;
Nope.string().between(3, 5).validate('42') // returns the error message;
Nope.string().between(3, 5).validate('magical') // 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