Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 94 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,35 +676,102 @@ function buildCode (location, code, laterCode, locationPath) {
code += `if (obj['${requiredProperty}'] === undefined) throw new Error('"${requiredProperty}" is required!')\n`
}

if (schema.allOf) {
const builtCode = buildCodeWithAllOfs(location, code, laterCode, locationPath)
code = builtCode.code
laterCode = builtCode.laterCode
}

return { code, laterCode }
}

function buildCodeWithAllOfs (location, code, laterCode, locationPath) {
if (location.schema.allOf) {
location.schema.allOf.forEach((ss) => {
const builtCode = buildCodeWithAllOfs(mergeLocation(location, { schema: ss }), code, laterCode, locationPath)
code = builtCode.code
laterCode = builtCode.laterCode
})
} else {
const builtCode = buildCode(location, code, laterCode, locationPath)
function mergeAllOfSchema (location, schema, mergedSchema) {
for (let allOfSchema of schema.allOf) {
if (allOfSchema.$ref) {
allOfSchema = refFinder(allOfSchema.$ref, mergeLocation(location, { schema: allOfSchema })).schema
}

code = builtCode.code
laterCode = builtCode.laterCode
}
let allOfSchemaType = allOfSchema.type
if (allOfSchemaType === undefined) {
allOfSchemaType = inferTypeByKeyword(allOfSchema)
}

return { code, laterCode }
if (allOfSchemaType !== undefined) {
if (
mergedSchema.type !== undefined &&
mergedSchema.type !== allOfSchemaType
) {
throw new Error('allOf schemas have different type values')
}
mergedSchema.type = allOfSchemaType
}

if (allOfSchema.format !== undefined) {
if (
mergedSchema.format !== undefined &&
mergedSchema.format !== allOfSchema.format
) {
throw new Error('allOf schemas have different format values')
}
mergedSchema.format = allOfSchema.format
}

if (allOfSchema.nullable !== undefined) {
if (
mergedSchema.nullable !== undefined &&
mergedSchema.nullable !== allOfSchema.nullable
) {
throw new Error('allOf schemas have different nullable values')
}
mergedSchema.nullable = allOfSchema.nullable
}

if (allOfSchema.properties !== undefined) {
if (mergedSchema.properties === undefined) {
mergedSchema.properties = {}
}
Object.assign(mergedSchema.properties, allOfSchema.properties)
}

if (allOfSchema.additionalProperties !== undefined) {
if (mergedSchema.additionalProperties === undefined) {
mergedSchema.additionalProperties = {}
}
Object.assign(mergedSchema.additionalProperties, allOfSchema.additionalProperties)
}

if (allOfSchema.patternProperties !== undefined) {
if (mergedSchema.patternProperties === undefined) {
mergedSchema.patternProperties = {}
}
Object.assign(mergedSchema.patternProperties, allOfSchema.patternProperties)
}

if (allOfSchema.required !== undefined) {
if (mergedSchema.required === undefined) {
mergedSchema.required = []
}
mergedSchema.required.push(...allOfSchema.required)
}

if (allOfSchema.oneOf !== undefined) {
if (mergedSchema.oneOf === undefined) {
mergedSchema.oneOf = []
}
mergedSchema.oneOf.push(...allOfSchema.oneOf)
}

if (allOfSchema.anyOf !== undefined) {
if (mergedSchema.anyOf === undefined) {
mergedSchema.anyOf = []
}
mergedSchema.anyOf.push(...allOfSchema.anyOf)
}

if (allOfSchema.allOf !== undefined) {
mergeAllOfSchema(location, allOfSchema, mergedSchema)
}
}
delete mergedSchema.allOf
}

function buildInnerObject (location, locationPath) {
const schema = location.schema
const result = buildCodeWithAllOfs(location, '', '', locationPath)
const result = buildCode(location, '', '', locationPath)
if (schema.patternProperties) {
const { code, laterCode } = addPatternProperties(location)
result.code += code
Expand Down Expand Up @@ -1030,6 +1097,13 @@ function buildValue (laterCode, locationPath, input, location) {
}
}

if (schema.allOf) {
const mergedSchema = clone(schema)
mergeAllOfSchema(location, schema, mergedSchema)
schema = mergedSchema
location.schema = mergedSchema
}

const type = schema.type
const nullable = schema.nullable === true

Expand Down
56 changes: 56 additions & 0 deletions test/allof.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,64 @@
'use strict'

const test = require('tap').test
const { DateTime } = require('luxon')
const build = require('..')

test('allOf: combine type and format ', (t) => {
t.plan(1)

const schema = {
allOf: [
{ type: 'string' },
{ format: 'time' }
]
}
const stringify = build(schema)
const date = new Date()
const value = stringify(date)
t.equal(value, `"${DateTime.fromJSDate(date).toFormat('HH:mm:ss')}"`)
})

test('allOf: combine additional properties ', (t) => {
t.plan(1)

const schema = {
allOf: [
{ type: 'object' },
{
type: 'object',
additionalProperties: { type: 'boolean' }
}
]
}
const stringify = build(schema)
const data = { property: true }
const value = stringify(data)
t.equal(value, JSON.stringify(data))
})

test('allOf: combine pattern properties', (t) => {
t.plan(1)

const schema = {
allOf: [
{ type: 'object' },
{
type: 'object',
patternProperties: {
foo: {
type: 'number'
}
}
}
]
}
const stringify = build(schema)
const data = { foo: 42 }
const value = stringify(data)
t.equal(value, JSON.stringify(data))
})

test('object with allOf and multiple schema on the allOf', (t) => {
t.plan(4)

Expand Down