From 763e41ca6e1d4d8d23e74ee7095b0643728c794e Mon Sep 17 00:00:00 2001 From: fogine <7884288+fogine@users.noreply.github.com> Date: Fri, 29 Jun 2018 17:55:50 +0200 Subject: [PATCH] adds $toJSON keyword --- lib/validator.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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,