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

Nope Object Documentation


shape

Sets the shape of the object. Use Nope validators as values

Signature:

shape(shape: object)

Example:

const schema = Nope.object().shape({
  name: Nope.string().atMost(15).required(),
  email: Nope.string().email("Please provide a valid email").required(),
});

const errors = schema.validate({
  name: "Test",
  email: "invalidemail",
});

console.log(errors); // { email: 'Please provide a valid email', }

extend

Extends the schema of an already defined NopeObject

Signature:

extend(base: NopeObject)

Example:

const baseSchema = Nope.object().shape({
  password: Nope.string().atLeast(5),
  confirmPassword: Nope.string()
    .oneOf([Nope.ref("password")], "Passwords don't match")
    .required(),
});

const userSchema = Nope.object()
  .extend(baseSchema)
  .shape({
    name: Nope.string().atLeast(4).required(),
  });

userSchema.validate({
  name: "Jonathan",
  password: "birdybird",
  confirmPassworod: "burdyburd",
}); // returns { confirmPassword: 'Passwords don\'t match' }

noUnknown

Return an error message if the entry contains keys that are not defined in the schema

Signature:

noUnknown(message: string)

Example:

const schema = Nope.object()
  .shape({
    name: Nope.string().atLeast(5),
  })
  .noUnknown("no unknown keys");

schema.validate({
  name: "Jonathan",
  password: "birdybird",
}); // returns 'no unknown keys';

validate

Run the validators against the passed entry. Note that this function is synchronous and will not resolve promises. For resolving promises see validateAsync

Signature:

validate(entry: object, context?: object, options?: { abortEarly?: boolean })

context - Outside params that you can reference in the schema options.abortEarly - If an error is found, stop executing the rest of the schema and return it

Example:

See examples above

validateAt

Run only one validator of the object

Signature:

validateAt(path: string, entry: object

Example:

const schema = Nope.object().shape({
  foo: Nope.array().of(
    Nope.object().shape({
      loose: Nope.boolean(),
      bar: Nope.string().when("loose", {
        is: true,
        then: Nope.string().max(5, "tooLong"),
        otherwise: Nope.string().min(5, "tooShort"),
      }),
    })
  ),
});

const rootValue = {
  foo: [{ bar: "123" }, { bar: "123456", loose: true }],
};

schema.validateAt("foo[0].bar", rootValue); // returns 'tooShort';
schema.validateAt("foo[1].bar", rootValue); // returns 'tooLong';

validateAsync

If you plan to use async validations, you should use this instead of validate

Signature:

validateAsync(entry: object, context?: object)

Example:

const schema = Nope.object().shape({
  username: Nope.string().test((str) => {
    if (str) {
      return Promise.resolve(undefined);
    }

    return Promise.reject("str");
  }),
});

const invalid = { username: undefined };
const valid = { username: "123" };
await schema.validateAsync(invalid); // returns { username: 'str' }

await schema.validateAsync(valid); // returns undefined
Clone this wiki locally