Skip to content

Adds anyOf support #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2017
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
46 changes: 42 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

var fastSafeStringify = require('fast-safe-stringify')
var Ajv = require('ajv')

var uglify = null
var isLong
Expand Down Expand Up @@ -87,10 +88,19 @@ function build (schema, options) {
code = uglifyCode(code)
}

var dependencies = []
var dependenciesName = []
if (hasAdditionalPropertiesTrue(schema)) {
return (new Function('fastSafeStringify', code))(fastSafeStringify)
dependencies.push(fastSafeStringify)
dependenciesName.push('fastSafeStringify')
}
return (new Function(code))()
if (hasAnyOf(schema)) {
dependencies.push(new Ajv())
dependenciesName.push('ajv')
}

dependenciesName.push(code)
return (Function.apply(null, dependenciesName).apply(null, dependencies))
}

function hasAdditionalPropertiesTrue (schema) {
Expand All @@ -107,6 +117,20 @@ function hasAdditionalPropertiesTrue (schema) {
return false
}

function hasAnyOf (schema) {
if ('anyOf' in schema) { return true }

var objectKeys = Object.keys(schema)
for (var i = 0; i < objectKeys.length; i++) {
var value = schema[objectKeys[i]]
if (typeof value === 'object') {
if (hasAnyOf(value)) { return true }
}
}

return false
}

function $asNull () {
return 'null'
}
Expand Down Expand Up @@ -517,8 +541,22 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
funcName = (name + key + subKey).replace(/[-.\[\]]/g, '') // eslint-disable-line
laterCode = buildArray(schema, laterCode, funcName, externalSchema, fullSchema)
code += `
json += ${funcName}(obj${accessor})
`
json += ${funcName}(obj${accessor})
`
break

case undefined:
if ('anyOf' in schema) {
schema.anyOf.forEach((s, index) => {
code += `
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(s, {depth: null})}, obj${accessor}))
${nested(laterCode, name, key, s, externalSchema, fullSchema, subKey).code}
`
})
code += `
else json+= null
`
} else throw new Error(`${schema} unsupported`)
break
default:
throw new Error(`${type} unsupported`)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Stringify your JSON at max speed",
"main": "index.js",
"scripts": {
"benchmark": "node bench.js",
"test": "standard && tap test/*.test.js"
},
"precommit": "test",
Expand Down Expand Up @@ -33,6 +34,7 @@
"uglify-es": "^3.1.1"
},
"dependencies": {
"ajv": "^5.2.3",
"fast-safe-stringify": "^1.2.0"
}
}
41 changes: 41 additions & 0 deletions test/anyof.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const test = require('tap').test
const build = require('..')

test('object with multiple types field', (t) => {
t.plan(2)

const schema = {
title: 'object with multiple types field',
type: 'object',
properties: {
str: {
'anyOf': [{
type: 'string'
}, {
type: 'boolean'
}]
}
}
}
const stringify = build(schema)

try {
const value = stringify({
str: 'string'
})
t.is(value, '{"str":"string"}')
} catch (e) {
t.fail()
}

try {
const value = stringify({
str: true
})
t.is(value, '{"str":true}')
} catch (e) {
t.fail()
}
})