Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Possible to have `validate` fail on not specified elements?

The below returns success, even though obj contains an element that is not in the schema.

Question

Is it possible to have validate fail, when it sees elements that are not specified in the schema?

Jest have this expect(obj1).toEqual(obj2).

If Validate can't do it, what options do I then have to detect unwanted elements?

const Schema = require("validate");

const obj = {good: "1", bad: "2"};

const user = new Schema({
  good: {
    type: String,
    required: true,
  }
});

const r = user.validate(obj);
console.log(r);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes, there is a strict option on the Schema, from the documentation:

Schema

A Schema defines the structure that objects should be validated against.

Parameters
  • obj schema definition
  • opts options
    • opts.typecast

    • opts.strip

    • opts.strict

      Validation fails when object contains properties not defined in the schema (optional, default false)

So you'll need something like:

const options = { strict: true };

const user = new Schema({
  good: {
    type: String,
    required: true,
  }
}, options);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!