Skip to content
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

$ref support #12

Merged
merged 5 commits into from
Oct 8, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 36 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@ function $asRegExp (reg) {
return '"' + reg + '"'
}

function addPatternProperties (pp, ap) {
function addPatternProperties (schema) {
var pp = schema.patternProperties
let code = `
var keys = Object.keys(obj)
for (var i = 0; i < keys.length; i++) {
if (properties[keys[i]]) continue
`
Object.keys(pp).forEach((regex, index) => {
if (pp[regex]['$ref']) {
pp[regex] = refFinder(pp[regex]['$ref'], schema)
}
var type = pp[regex].type
code += `
if (/${regex}/.test(keys[i])) {
Expand Down Expand Up @@ -189,8 +193,8 @@ function addPatternProperties (pp, ap) {
}
`
})
if (ap) {
code += additionalProperty(ap)
if (schema.additionalProperties) {
code += additionalProperty(schema)
}

code += `
Expand All @@ -199,13 +203,18 @@ function addPatternProperties (pp, ap) {
return code
}

function additionalProperty (ap) {
function additionalProperty (schema) {
var ap = schema.additionalProperties
let code = ''
if (ap === true) {
return `
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
`
}
if (ap['$ref']) {
ap = refFinder(ap['$ref'], schema)
}

let type = ap.type
if (type === 'object') {
code += buildObject(ap, '', 'buildObjectAP')
Expand Down Expand Up @@ -241,26 +250,41 @@ function additionalProperty (ap) {
return code
}

function addAdditionalProperties (ap) {
function addAdditionalProperties (schema) {
return `
var keys = Object.keys(obj)
for (var i = 0; i < keys.length; i++) {
if (properties[keys[i]]) continue
${additionalProperty(ap)}
${additionalProperty(schema)}
}
`
}

function refFinder (ref, schema) {
// Split file from walk
ref = ref.split('#')
// If external file
if (ref[0]) {
schema = require(ref[0])
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never do this, it's sync and done in a place where it might cause to bugs. We should not load files into Node. However, we should use an approach similar to https://www.npmjs.com/package/is-my-json-valid#external-schemas.

const walk = ref[1].split('/')
let code = 'return schema'
for (let i = 1; i < walk.length; i++) {
code += `['${walk[i]}']`
}
return (new Function('schema', code))(schema)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!!

}

function buildObject (schema, code, name) {
code += `
function ${name} (obj) {
var json = '{'
`

if (schema.patternProperties) {
code += addPatternProperties(schema.patternProperties, schema.additionalProperties)
code += addPatternProperties(schema)
} else if (schema.additionalProperties && !schema.patternProperties) {
code += addAdditionalProperties(schema.additionalProperties)
code += addAdditionalProperties(schema)
}

var laterCode = ''
Expand All @@ -273,6 +297,10 @@ function buildObject (schema, code, name) {
json += '${$asString(key)}:'
`

if (schema.properties[key]['$ref']) {
schema.properties[key] = refFinder(schema.properties[key]['$ref'], schema)
}

const result = nested(laterCode, name, '.' + key, schema.properties[key])

code += result.code
Expand Down
12 changes: 12 additions & 0 deletions test/ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"definitions": {
"def": {
"type": "object",
"properties": {
"str": {
"type": "string"
}
}
}
}
}
221 changes: 221 additions & 0 deletions test/ref.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
'use strict'

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

test('ref internal - properties', (t) => {
t.plan(2)

const schema = {
title: 'object with $ref',
definitions: {
def: {
type: 'object',
properties: {
str: {
type: 'string'
}
}
}
},
type: 'object',
properties: {
obj: {
$ref: '#/definitions/def'
}
}
}

const object = {
obj: {
str: 'test'
}
}

const stringify = build(schema)
const output = stringify(object)

try {
JSON.parse(output)
t.pass()
} catch (e) {
t.fail()
}

t.equal('{"obj":{"str":"test"}}', output)
})

test('ref external - properties', (t) => {
t.plan(2)

const schema = {
title: 'object with $ref',
definitions: {
def: {
type: 'object',
properties: {
str: {
type: 'string'
}
}
}
},
type: 'object',
properties: {
obj: {
$ref: __dirname + '/ref.json#/definitions/def' // eslint-disable-line
}
}
}

const object = {
obj: {
str: 'test'
}
}

const stringify = build(schema)
const output = stringify(object)

try {
JSON.parse(output)
t.pass()
} catch (e) {
t.fail()
}

t.equal('{"obj":{"str":"test"}}', output)
})

test('ref internal - patternProperties', (t) => {
t.plan(2)

const schema = {
title: 'object with $ref',
definitions: {
def: {
type: 'object',
properties: {
str: {
type: 'string'
}
}
}
},
type: 'object',
properties: {},
patternProperties: {
obj: {
$ref: '#/definitions/def'
}
}
}

const object = {
obj: {
str: 'test'
}
}

const stringify = build(schema)
const output = stringify(object)

try {
JSON.parse(output)
t.pass()
} catch (e) {
t.fail()
}

t.equal('{"obj":{"str":"test"}}', output)
})

test('ref internal - additionalProperties', (t) => {
t.plan(2)

const schema = {
title: 'object with $ref',
definitions: {
def: {
type: 'object',
properties: {
str: {
type: 'string'
}
}
}
},
type: 'object',
properties: {},
additionalProperties: {
$ref: '#/definitions/def'
}
}

const object = {
obj: {
str: 'test'
}
}

const stringify = build(schema)
const output = stringify(object)

try {
JSON.parse(output)
t.pass()
} catch (e) {
t.fail()
}

t.equal('{"obj":{"str":"test"}}', output)
})

test('ref internal - pattern-additional Properties', (t) => {
t.plan(2)

const schema = {
title: 'object with $ref',
definitions: {
def: {
type: 'object',
properties: {
str: {
type: 'string'
}
}
}
},
type: 'object',
properties: {},
patternProperties: {
reg: {
$ref: '#/definitions/def'
}
},
additionalProperties: {
$ref: '#/definitions/def'
}
}

const object = {
reg: {
str: 'test'
},
obj: {
str: 'test'
}
}

const stringify = build(schema)
const output = stringify(object)

try {
JSON.parse(output)
t.pass()
} catch (e) {
t.fail()
}

t.equal('{"reg":{"str":"test"},"obj":{"str":"test"}}', output)
})