diegohaz / bodymen

Body parser middleware for MongoDB, Express and Nodejs (MEN)
Other
47 stars 15 forks source link

A required field should be ignored in case of update #18

Closed thxmxx closed 6 years ago

thxmxx commented 6 years ago

Most of the time the Required statement is only applicable for creating the documents. After creation I cannot force the user to fill these fields again. Is there any option to bypass this?

I am copying the schema tree

const schemaTree = schema.tree
delete schemaTree._id;
delete schemaTree.updatedAt;
delete schemaTree.createdAt;
delete schemaTree.__v;

router.put('/:id',
  body( schemaTree ),
  update)
diegohaz commented 6 years ago

You can override the option when passing the object to body:

body({ ...schemaTree, field: { ...scemaTree.field, required: false } })
Mustafax06 commented 5 years ago

Do I have to do this to all fields? Can I just override them all at once?