diff --git a/lib/validator.js b/lib/validator.js index d0bf2a4..4a02afd 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -42,6 +42,26 @@ ajv.addKeyword('relation', { } }); +ajv.addKeyword('$toJSON', { + modifying: true, + statements: true, + valid: true, + metaSchema: { + type: 'object' + }, + validate: function(schema, data, parentSchema, dataPath, parentData, prop) { + if ( typeof data === 'object' + && data !== null + && typeof data.toJSON === 'function' + && parentData + ) { + parentData[prop] = data.toJSON(); + } else if (typeof data === 'string') { + parentData[prop] = JSON.parse(data); + } + } +}); + ajv.addKeyword('$toDate', { type: ['string', 'integer'], modifying: true,