Skip to content
Bruno Vego edited this page Jun 1, 2021 · 5 revisions

Nope Number Documentation


integer

Asserts if the entry is an integer

Signature:

integer(message: string)

Example:

Nope.number().integer("error message").validate(2); // returns undefined

Nope.number().integer("error message").validate(4.2); // returns the error message

min

alias for greaterThan

Signature:

min(size: number, message: string)

greaterThan

Asserts if the entry is smaller than a threshold

Signature:

greaterThan(size: number, message: string)

Example:

Nope.number().greaterThan(1, "error message").validate(2); // returns undefined

Nope.number().greaterThan(1, "error message").validate(1); // returns the error message

max

alias for lessThan

Signature:

max(size: number, message: string)

lessThan

Asserts if the entry is greater than a threshold

Signature:

lessThan(size: number, message: string)

Example:

Nope.number().lessThan(1, "error message").validate(-1); // returns undefined

Nope.number().lessThan(1, "error message").validate(2); // returns the error message

positive

Asserts if the entry is positive

Signature:

positive(message: string)

Example:

Nope.number()
  .positive('error message')
  .validate(42); // returns undefined

Nope.number()
  .positive('error message')
  .validate(-42); // returns the error message

negative

Asserts if the entry is negative

Signature:

negative(message: string)

Example:

Nope.number()
  .negative('error message')
  .validate(-42); // returns undefined

Nope.number()
  .negative('error message')
  .validate(42); // returns the error message

between

Asserts if the entry is between a range

Signature:

public between(sizeStart: number, sizeEnd: number, atLeastMessage: String, atMostMessage: String)

Example:

Nope.string().between(3, 5).validate(4) // returns undefined;
Nope.string().between(3, 5).validate(1) // returns the error message;
Nope.string().between(3, 5).validate(42) // returns the error message;

when

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",
  },
});

oneOf

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

notOneOf

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

required

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

notAllowed

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

test

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

validate

Runs the rule chain against an entry

Signature:

validate(entry: string | undefined | null)

Can be seen in use in the examples above