Thursday, 7 December 2017

Objection.js, validation and sanification

Hi, I am trying Objection.js and I love it. Until now, while testing the module, I have performed object validation as in the example, through JSON-schema.Assume I have this:class Person extends Model { static get jsonSchema() { return { type: 'object', required: ['firstName', 'lastName'], properties: { id: {type: 'integer'}, parentId: {type: ['integer', 'null']}, firstName: {type: 'string', minLength: 1, maxLength: 20}, lastName: {type: 'string', minLength: 1, maxLength: 20}, age: {type: 'number'}, address: { type: 'object', properties: { street: {type: 'string'}, city: {type: 'string'}, zipCode: {type: 'string'} } } } }; } } This works, but I have a few doubts.I do not understand if it is possible to specify custom error messages. For instance, maybe I would like to have "first name length must be between 1 and 20" as the error message sent out when the length is wrong.I know it is possible, but I have not understood how to change the validator with a custom one (I would like to try Joi). Maybe someone has an example somewhere?

Submitted December 07, 2017 at 02:52PM by honestserpent

No comments:

Post a Comment