Skip to content

Instantly share code, notes, and snippets.

@smalltotem
Created January 27, 2015 17:30
Show Gist options
  • Save smalltotem/e859c7d9ac33f763fe2d to your computer and use it in GitHub Desktop.
Save smalltotem/e859c7d9ac33f763fe2d to your computer and use it in GitHub Desktop.
Joi validation for and array of objects. with null and undefined values.
#!/bin/env node
var Joi = require('joi');
var payload = [{ name: 'TEST Name 1',
source_id: '10104',
type: 'Bike',
state: 'building',
points: undefined,
assignee_name: 'bobby tables',
created_at: '2015-01-12T20:11:34+00:00' },
{ name: 'yep',
source_id: '10201',
type: 'auto',
state: 'building',
points: null,
assignee_name: 'bobby tables',
created_at: '2015-01-21T20:20:31+00:00' }];
var validation = Joi.array().includes(Joi.object().keys({
name: Joi.string(),
source_id: Joi.string(),
type: Joi.string(),
state: Joi.string(),
points: Joi.number().integer().optional().allow(null),
assignee_name: Joi.string(),
created_at: Joi.date(),
project_id: Joi.number().integer()
})
);
Joi.validate(payload, validation, function (err, value) {
console.log('err', err);
console.log('value', value);
});
@ashrafkm
Copy link

ashrafkm commented Aug 4, 2017

while inserting data created_at should be autoinsterted with date and time. How can I do that?

@ashrafkm
Copy link

Hi, Is it possible to make id as auoinsert.
here is my schema

const ratingSchema = Joi.object().keys({
     id: Joi.number().integer(),
     ratings: Joi.number().precision(2),
});` 

In this id should be autoinserted. How can I make

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment