Node.js/Jest

[Node] Best JavaScript code snippets using joi.date

brightlightkim 2022. 4. 2. 03:08

Best JavaScript code snippets using joi.date

cli/templates/models/schemas/foo.js/Joi.object.keys

Joi.object().keys({
  id         : Joi.number().integer(),
  firstname  : Joi.string().max(128).optional(),
  lastname   : Joi.string().max(128).optional(),
  created_at : Joi.date().default(() => moment().format(), 'date created'),
  updated_at : Joi.date().default(() => moment().format(), 'date updated'),
})

origin: rsemburakkiannan/employee-microservice-node

 

app/employee-model.js/Employee/schema

// schema validation using joi
 static schema () {
  return {
   employeeNumber: joi.number().integer(),
   firstname: joi.string().alphanum().min(3).max(30).required(),
   lastname: joi.string().alphanum().min(3).max(30).required(),
   grade: joi.string().regex(/^[a-zA-Z0-9]{3}$/).required(),
   birthDate: joi.date().raw().required(),
   joinDate: joi.date().raw().required(),
   salary: joi.number().required(),
   email: joi.string().email()
  }
 }

origin: jkbhatnagar/dev-samples-aws-serverless-nodejs-mongodb-restapi

 

api/products.js/validateProduct

function validateProduct(data, callback){
 const schema = Joi.object().keys({
  id: Joi.string().guid().required(),
  title: Joi.string().min(3).max(50).required(),
  description: Joi.string().min(3).max(50),
  unit: Joi.string().min(1).max(10).required(),
  price: Joi.number().positive().greater(0).required(),
  updatedat: Joi.date().timestamp(),
 });

 Joi.validate(data, schema, (err, value) => {
  if (err) {
   callback(err);
  }
  else{
   callback(null);
  }
 });
}

origin: gladchinda/joi-schema-validation-sourcecode

 

app.js/app.post

birthday: Joi.date().max('1-1-2004').iso(),