Ottoman v2 validation

Hi,

As mention in https://v2.ottomanjs.com/guides/document.html#updating,

The save() function is generally the right way to update a document with Ottoman. With save() , you get full validation and middleware.

The validation of field name is not working. Expected an error on the field name firstNames but resulted in saving into database.

const User = ottoman.model(
  "User",
  {
    firstName: String,
    lastName: String,
    email: String,
    tagline: String,
    hide: Boolean,
    dob: Date,
  },
  { idkey: "id" }
);

const tom = new User({
  id: "a2",
  firstNames: "Major",
  lastName: "Tom",
  email: "major.tom@acme.com",
  tagLine: "Send me up a drink",
});

const runAsync = async () => {
  try {
    await tom.save();
    console.log(`success: user ${tom.firstNames} added!`);
  } catch (err) {
    throw err;
  }
};

Hello @Clive,

For FirstName to be validated and made required, you will have to flag it as required
const User = ottoman.model(
“User”,
{
firstName: { type: String, required:true},
lastName: String,
email: String,
tagline: String,
hide: Boolean,
dob: Date,
},
{ idkey: “id” }
);

Validation will trigger when you update or save the document.

1 Like