Skip to content
Ademílson Tonato edited this page Jun 12, 2026 · 8 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' }

pick

Creates a new object schema containing only the selected keys. The original schema is not mutated. Non-existing keys are silently ignored.

Signature:

pick(keys: string[]): NopeObject

Example:

const userSchema = Nope.object().shape({
  name: Nope.string().required(),
  email: Nope.string().email(),
  password: Nope.string().required(),
});

const credentialsSchema = userSchema.pick(["email", "password"]);

omit

Creates a new object schema excluding the selected keys. The original schema is not mutated. Non-existing keys are silently ignored.

Signature:

omit(keys: string[]): NopeObject

Example:

const publicUserSchema = userSchema.omit(["password"]);

stripUnknown

Removes unknown keys from the input during validation instead of failing. Does not mutate the original input object.

Nested objects only strip unknown keys if they also use stripUnknown().

Signature:

stripUnknown()

Example:

const schema = Nope.object()
  .shape({ name: Nope.string().required() })
  .stripUnknown();

schema.validate({ name: "Lucas", admin: true }); // returns undefined

getDefault

Collects default values from child field schemas. Returns undefined if no child defines a default.

Signature:

getDefault(): object | undefined

Example:

const schema = Nope.object().shape({
  name: Nope.string().default("Anonymous"),
  active: Nope.boolean().default(true),
});

schema.getDefault(); // { name: "Anonymous", active: true }

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';

isValid

Returns a Promise<boolean> indicating whether the object passes validation. Does not throw validation errors.

Signature:

isValid(entry: object, context?: object): Promise<boolean>

Example:

const schema = Nope.object().shape({
  email: Nope.string().email(),
});

await schema.isValid({ email: "test@example.com" }); // true
await schema.isValid({ email: "bad" }); // false

isValidSync

Synchronous version of isValid().

Signature:

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

Example:

schema.isValidSync({ email: "test@example.com" }); // true
schema.isValidSync({ email: "bad" }); // false

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