- enforce business logic
- preserve data integrity
- avert malicious inputs
Ex: graphql-constraint-directive (featured in Apollo Blog)
Pros
- GraphQL centric and versatile
Cons
- Does not allow for
@constrainton query args - No compat with
apollo-serverv2, quirks in v1
Ex: validating args.id for an ObjectID
Pros
- Simple and intuitive
Cons
- Too repetitive, not DRY
- Clutters up, doesn't scale
Ex: pure funcs using validator.js
Pros
- Simple enough, composable, can scale
Cons
- Reinventing the wheel
- Code to maintain
Ex: built-in validators in Mongoose
Pros
- Common checks
- Extensible
- define custom validation logic
- customize error messages
Cons
- Too many gotchas, e.g.
uniqueis not a validator, but a unique indexupdate*validators are off by default- many
pre/posthooks don't fire forupdate*
- Hard to validate a subset of fields
- e.g.
findByIdthensavewill validate all fields
- e.g.
- Models grow out of size
Ex:
Pros
- Very expressive & readable
- DRY, doesn't clutter up
- Extensible & customizable
- custom validators, plugins, messages
Cons
- Cryptic error messages (though customizable)