-
-
Notifications
You must be signed in to change notification settings - Fork 14
String
- regex
- url
- min
- greaterThan
- max
- lessThan
- exactLength
- length
- uuid
- between
- default
- getDefault
- nullable
- nonNullable
- defined
- optional
- notRequired
- isValid
- isValidSync
- 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 messageAsserts 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 messageAsserts if the entry is a valid email
Signature:
email(message: string)
Example:
Nope.string().email().validate("test@gmail.com"); // returns undefined
Nope.string().email().validate("testgmail.com"); // returns the error messagealias 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 messagealias 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 messageAsserts 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 messageAlias for exactLength. Validates that the string has exactly the given number of characters.
Signature:
length(size: number, message?: string)
Example:
Nope.string().length(5).validate("hello"); // returns undefined
Nope.string().length(5).validate("hi"); // returns the error messageAsserts if the entry is a valid UUID string (v1, v4, v5, and other standard formats).
Signature:
uuid(message?: string)
Example:
Nope.string().uuid().validate("550e8400-e29b-41d4-a716-446655440000"); // returns undefined
Nope.string().uuid("bad id").validate("not-a-uuid"); // returns "bad id"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;Set a fallback value used when the input is undefined.
Signature:
default(value: T)
Defaults apply only when the input is undefined. They do not replace null, empty strings, false, 0, or empty arrays.
Example:
const schema = Nope.string().default("Anonymous");
schema.validate(undefined); // returns undefined (uses "Anonymous")
schema.validate(null); // does not use the defaultReturns the value set by default(), or undefined if no default was configured.
Signature:
getDefault(): T | undefined
Example:
Nope.string().default("Anonymous").getDefault(); // "Anonymous"
Nope.string().getDefault(); // undefinedAllows null as a valid value. Does not automatically allow undefined.
Signature:
nullable()
Example:
Nope.string().nullable().validate(null); // returns undefined
Nope.string().nullable().defined().validate(undefined); // returns error messageRejects null. This is the default behavior unless nullable() was called.
Signature:
nonNullable(message?: string)
Example:
Nope.string().nonNullable("no null").validate(null); // returns "no null"Rejects undefined. Does not affect null handling — use nullable() / nonNullable() for that.
Signature:
defined(message?: string)
Example:
Nope.string().defined("must be defined").validate(undefined); // returns "must be defined"
Nope.string().defined().validate("hello"); // returns undefinedAllows undefined. Does not automatically allow or reject null.
Signature:
optional()
Example:
Nope.string().optional().email().validate(undefined); // returns undefined
Nope.string().optional().email().validate("not-an-email"); // returns error messageAlias for optional().
Signature:
notRequired()
Example:
Nope.string().notRequired().validate(undefined); // returns undefinedReturns a Promise<boolean> indicating whether the value passes validation. Does not throw validation errors.
Signature:
isValid(entry?: T, context?: object): Promise<boolean>
Example:
const schema = Nope.string().email();
await schema.isValid("test@example.com"); // true
await schema.isValid("invalid"); // falseSynchronous version of isValid(). Returns a boolean and does not throw validation errors.
Signature:
isValidSync(entry?: T, context?: object): boolean
Example:
const schema = Nope.string().email();
schema.isValidSync("test@example.com"); // true
schema.isValidSync("invalid"); // falseConditional 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 messageAsserts 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 messageAsserts 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 messageAsserts 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 messageAdd 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 messageRuns the rule chain against an entry
Signature:
validate(entry: string | undefined | null)
Can be seen in use in the examples above