-
-
Notifications
You must be signed in to change notification settings - Fork 207
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
$ref support #12
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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])) { | ||
|
@@ -189,8 +193,8 @@ function addPatternProperties (pp, ap) { | |
} | ||
` | ||
}) | ||
if (ap) { | ||
code += additionalProperty(ap) | ||
if (schema.additionalProperties) { | ||
code += additionalProperty(schema) | ||
} | ||
|
||
code += ` | ||
|
@@ -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') | ||
|
@@ -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]) | ||
} | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = '' | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"definitions": { | ||
"def": { | ||
"type": "object", | ||
"properties": { | ||
"str": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.