FieldsValidator
FieldsValidator is useful to make validation of data with different sources like a mongoose model, an array of required fields, ... With this npm you can dramatically reduce your code and your replication code
$ npm install fieldsValidator
Examples
+ With a mongoose model
function isValidWithMongo(Model, fields, [omitRequired, omissions])
Arguments
Model
(Object): the mongoose model referencefields
(Object): the fields you want to check and validateomitRequired
(Boolean): if you set this one, it will ignore the required fields (for a put for example)omissions
(Array): values to omit in the schema of the Model
Here is an exemple of what you had before :
var User = mongoose; { if !reqbodypassword return resstatus400; else if typeof reqbodypassword !== 'string' return resstatus400; if !reqbodyemail return resstatus400; else if typeof reqbodyemail !== 'string' return resstatus400; ... User; ...}
And what you'll have with fieldsValidator :
var User = mongoose; { var error = fieldsValidator; if error return resstatus400; ... User; ...}
function getValuesInSchema(model, [additions, omissions])
Arguments
Model
(Object): the mongoose model referenceadditions
(Array | String): the customs fields you want to get in addition in your listomissions
(Array): values to omit in the schema of the Model
Returns
(Array): values in your mongoose schema (Attention: _id & __v is omited by default)
Example:
var User = mongoose;fieldsValidator;// -> ['lastname', 'firstname', ... ]
function createWithMongo(model, body, [additions, omissions])
Arguments
Model
(Object): the mongoose model referencebody
(Object): the object you want to fetch information with mongoose model argumentsadditions
(Array | String): the customs fields you want to get in addition in your object bodyomissions
(Array): values to omit in the schema of the Model
Returns
(Object): Object formated (Attention: _id & __v is omited by default)
Example:
Here is an exemple of what you had before :
var User = mongoose; { var newUser = firstname: reqbodyfirstname lastname: reqbodylastname password: reqbodypassword ;}
And what you'll have with fieldsValidator :
var User = mongoose; { var newUser = fieldsValidator;}
The main asset in this case is you don't have to update all of your code when you're updating your mongoose schema
+ With the docs of your swagger-ui
Use your docs to help your logic
function isValidWithSwagger(swaggerParameters, fields, [omitRequired])
Arguments
swaggerParameters
(Array): the array which contained the fields of your methods in your swagger spec filesfields
(Object): the fields you want to check and validateomitRequired
(Boolean): if you set this one, it will ignore the required fields (for a put for example)
Here is an exemple of a swagger spec files :
paths: '/login': post: tags: 'auth' summary: 'Login' operationId: 'login' parameters: in : 'formData' name: 'email' description: 'email to login' required: true type: 'string' in : 'formData' name: 'password' description: 'password to login' required: true type: 'string'
And what you'll have with fieldsValidator :
var User = mongoose; { var error = fieldsValidator; if error return resstatus400; ... User; ...}
+ With an array of custom required fields
function isValidWithCustoms(requiredFields, fields)
Arguments
requiredFields
(Array): the array which contained the custom required fieldsfields
(Object): the fields you want to check and validate
Here is an exemple of what you had before :
var User = mongoose; { if !reqbodypassword return resstatus400; else if typeof reqbodypassword !== 'string' return resstatus400; if !reqbodyemail return resstatus400; else if typeof reqbodyemail !== 'string' return resstatus400; ... User; ...}
And what you'll have with fieldsValidator :
var User = mongoose; { var error = fieldsValidator; if error return resstatus400; ... User; ...}
Roadmap
- Make a function to fetch automatically the configs of your swagger
Feel free to contribute
Made by Coenen Benjamin with love
See you on Lapetitesoeur