From ec136f87e28c902cbc66dd031da840ae11af8b55 Mon Sep 17 00:00:00 2001 From: Alexander Kureniov Date: Tue, 24 Jul 2018 10:35:07 +0300 Subject: [PATCH] fix: fix nested allOf handling --- index.js | 24 +++++++++++-------- package.json | 4 +++- test/allof.test.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 62abccc6..445a1034 100644 --- a/index.js +++ b/index.js @@ -457,18 +457,10 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) { return { code: code, laterCode: laterCode } } -function buildInnerObject (schema, name, externalSchema, fullSchema) { - var laterCode = '' - var code = '' - if (schema.patternProperties) { - code += addPatternProperties(schema, externalSchema, fullSchema) - } else if (schema.additionalProperties && !schema.patternProperties) { - code += addAdditionalProperties(schema, externalSchema, fullSchema) - } - +function buildCodeWithAllOfs (schema, code, laterCode, name, externalSchema, fullSchema) { if (schema.allOf) { schema.allOf.forEach((ss) => { - var builtCode = buildCode(ss, code, laterCode, name, externalSchema, fullSchema) + var builtCode = buildCodeWithAllOfs(ss, code, laterCode, name, externalSchema, fullSchema) code = builtCode.code laterCode = builtCode.laterCode @@ -483,6 +475,18 @@ function buildInnerObject (schema, name, externalSchema, fullSchema) { return { code: code, laterCode: laterCode } } +function buildInnerObject (schema, name, externalSchema, fullSchema) { + var laterCode = '' + var code = '' + if (schema.patternProperties) { + code += addPatternProperties(schema, externalSchema, fullSchema) + } else if (schema.additionalProperties && !schema.patternProperties) { + code += addAdditionalProperties(schema, externalSchema, fullSchema) + } + + return buildCodeWithAllOfs(schema, code, laterCode, name, externalSchema, fullSchema) +} + function addIfThenElse (schema, name, externalSchema, fullSchema) { var code = '' var r diff --git a/package.json b/package.json index f3229ecc..8679d403 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "index.js", "scripts": { "benchmark": "node bench.js", - "test": "standard && tap -j4 test/*.test.js" + "lint": "standard", + "unit": "tap -j4 test/*.test.js", + "test": "npm run lint && npm run unit" }, "precommit": "test", "repository": { diff --git a/test/allof.test.js b/test/allof.test.js index af101c1a..1c711b38 100644 --- a/test/allof.test.js +++ b/test/allof.test.js @@ -125,3 +125,62 @@ test('object with allOf and no schema on the allOf', (t) => { t.is(e.message, 'schema is invalid: data.allOf should NOT have less than 1 items') } }) + +test('object with nested allOfs', (t) => { + t.plan(1) + + const schema = { + title: 'object with nested allOfs', + type: 'object', + allOf: [ + { + required: [ + 'id' + ], + type: 'object', + properties: { + id1: { + type: 'integer' + } + } + }, + { + allOf: [ + { + type: 'object', + properties: { + id2: { + type: 'integer' + } + } + }, + { + type: 'object', + properties: { + id3: { + type: 'integer' + } + } + } + ] + } + ] + } + + try { + const stringify = build(schema) + try { + const value = stringify({ + id1: 1, + id2: 2, + id3: 3, + id4: 4 // extra prop shouldn't be in result + }) + t.is(value, '{"id1":1,"id2":2,"id3":3}') + } catch (e) { + t.fail() + } + } catch (e) { + t.fail() + } +})