From 6adb82449a203eba21468dc734915b4cb9cd5678 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 2 Nov 2022 19:18:12 -0400 Subject: [PATCH] Use "id" as the identifier property for Draft3 See: https://github.com/sourcemeta/alterschema/issues/83 Signed-off-by: Juan Cruz Viotti --- bindings/node/index.js | 2 +- bindings/node/jsonschema.js | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/bindings/node/index.js b/bindings/node/index.js index a6030cf..baa4a1c 100644 --- a/bindings/node/index.js +++ b/bindings/node/index.js @@ -29,7 +29,7 @@ async function transformer (root, path, ruleset, trails, originalSchema, from) { return true } - if (_.has(root, trail.path.concat(from === 'draft4' ? 'id' : '$id'))) { + if (_.has(root, trail.path.concat(jsonschema.getIdProperty(from)))) { return false } } diff --git a/bindings/node/jsonschema.js b/bindings/node/jsonschema.js index 9ad6ef6..c9e7c07 100644 --- a/bindings/node/jsonschema.js +++ b/bindings/node/jsonschema.js @@ -13,8 +13,12 @@ exports.usesVocabulary = (_root, value, _vocabulary) => { return false } +exports.getIdProperty = (version) { + return [ 'draft3', 'draft4' ].includes(version) ? 'id' : '$id' +} + exports.matches = async (schema, value) => { - const idProperty = METASCHEMAS[schema.$schema] === 'draft4' ? 'id' : '$id' + const idProperty = exports.getIdProperty(METASCHEMAS[schema.$schema]) jsonschema.add(schema) const result = await jsonschema.validate(jsonschema.get(schema[idProperty]), value) return result.valid