This package is no longer maintained.
Translates Mongoose schema to Joi. You can use Joi schema to do the validation. The idea is to write database models once and validate everywhere.
npm install mongoose-to-joi-translator
- All types
- required
- valid (enum validation)
- default
- Strings
- min
- Arrays
- items (element types)
- Numbers
- min
- max
- Objects
- Dates
- ObjectIDs
- Booleans
- Custom sync validators as documented here
Deeply nested document validation is supported, i.e. Objects within Objects, Arrays within Objects etc.
npm test
// Require the library
const getJoiSchema = require('mongoose-to-joi-translator');
const schema = new Schema({ word: { type: String } });
const ModelName = mongoose.model('ModelName', schema);
// Extract schema
const joiSchema = getJoiSchema(ModelName);
// Use Joi to validate
const { error, value } = Joi.validate({ word: 'hello' }, joiSchema);