-
-
Notifications
You must be signed in to change notification settings - Fork 205
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 all 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
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 |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
|
||
const fastSafeStringify = require('fast-safe-stringify') | ||
|
||
function build (schema) { | ||
function build (schema, options) { | ||
options = options || {} | ||
/* eslint no-new-func: "off" */ | ||
var code = ` | ||
'use strict' | ||
|
@@ -26,7 +27,7 @@ function build (schema) { | |
switch (schema.type) { | ||
case 'object': | ||
main = '$main' | ||
code = buildObject(schema, code, main) | ||
code = buildObject(schema, code, main, options.schema) | ||
break | ||
case 'string': | ||
main = $asString.name | ||
|
@@ -43,7 +44,7 @@ function build (schema) { | |
break | ||
case 'array': | ||
main = '$main' | ||
code = buildArray(schema, code, main) | ||
code = buildArray(schema, code, main, options.schema) | ||
break | ||
default: | ||
throw new Error(`${schema.type} unsupported`) | ||
|
@@ -141,24 +142,28 @@ function $asRegExp (reg) { | |
return '"' + reg + '"' | ||
} | ||
|
||
function addPatternProperties (pp, ap) { | ||
function addPatternProperties (schema, externalSchema) { | ||
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, externalSchema) | ||
} | ||
var type = pp[regex].type | ||
code += ` | ||
if (/${regex}/.test(keys[i])) { | ||
` | ||
if (type === 'object') { | ||
code += buildObject(pp[regex], '', 'buildObjectPP' + index) | ||
code += buildObject(pp[regex], '', 'buildObjectPP' + index, externalSchema) | ||
code += ` | ||
json += $asString(keys[i]) + ':' + buildObjectPP${index}(obj[keys[i]]) + ',' | ||
` | ||
} else if (type === 'array') { | ||
code += buildArray(pp[regex], '', 'buildArrayPP' + index) | ||
code += buildArray(pp[regex], '', 'buildArrayPP' + index, externalSchema) | ||
code += ` | ||
json += $asString(keys[i]) + ':' + buildArrayPP${index}(obj[keys[i]]) + ',' | ||
` | ||
|
@@ -189,8 +194,8 @@ function addPatternProperties (pp, ap) { | |
} | ||
` | ||
}) | ||
if (ap) { | ||
code += additionalProperty(ap) | ||
if (schema.additionalProperties) { | ||
code += additionalProperty(schema, externalSchema) | ||
} | ||
|
||
code += ` | ||
|
@@ -199,21 +204,26 @@ function addPatternProperties (pp, ap) { | |
return code | ||
} | ||
|
||
function additionalProperty (ap) { | ||
function additionalProperty (schema, externalSchema) { | ||
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, externalSchema) | ||
} | ||
|
||
let type = ap.type | ||
if (type === 'object') { | ||
code += buildObject(ap, '', 'buildObjectAP') | ||
code += buildObject(ap, '', 'buildObjectAP', externalSchema) | ||
code += ` | ||
json += $asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]]) + ',' | ||
` | ||
} else if (type === 'array') { | ||
code += buildArray(ap, '', 'buildArrayAP') | ||
code += buildArray(ap, '', 'buildArrayAP', externalSchema) | ||
code += ` | ||
json += $asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]]) + ',' | ||
` | ||
|
@@ -241,26 +251,41 @@ function additionalProperty (ap) { | |
return code | ||
} | ||
|
||
function addAdditionalProperties (ap) { | ||
function addAdditionalProperties (schema, externalSchema) { | ||
return ` | ||
var keys = Object.keys(obj) | ||
for (var i = 0; i < keys.length; i++) { | ||
if (properties[keys[i]]) continue | ||
${additionalProperty(ap)} | ||
${additionalProperty(schema, externalSchema)} | ||
} | ||
` | ||
} | ||
|
||
function buildObject (schema, code, name) { | ||
function refFinder (ref, schema, externalSchema) { | ||
// Split file from walk | ||
ref = ref.split('#') | ||
// If external file | ||
if (ref[0]) { | ||
schema = externalSchema[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, externalSchema) { | ||
code += ` | ||
function ${name} (obj) { | ||
var json = '{' | ||
` | ||
|
||
if (schema.patternProperties) { | ||
code += addPatternProperties(schema.patternProperties, schema.additionalProperties) | ||
code += addPatternProperties(schema, externalSchema) | ||
} else if (schema.additionalProperties && !schema.patternProperties) { | ||
code += addAdditionalProperties(schema.additionalProperties) | ||
code += addAdditionalProperties(schema, externalSchema) | ||
} | ||
|
||
var laterCode = '' | ||
|
@@ -273,7 +298,11 @@ function buildObject (schema, code, name) { | |
json += '${$asString(key)}:' | ||
` | ||
|
||
const result = nested(laterCode, name, '.' + key, schema.properties[key]) | ||
if (schema.properties[key]['$ref']) { | ||
schema.properties[key] = refFinder(schema.properties[key]['$ref'], schema, externalSchema) | ||
} | ||
|
||
const result = nested(laterCode, name, '.' + key, schema.properties[key], externalSchema) | ||
|
||
code += result.code | ||
laterCode = result.laterCode | ||
|
@@ -308,15 +337,15 @@ function buildObject (schema, code, name) { | |
return code | ||
} | ||
|
||
function buildArray (schema, code, name) { | ||
function buildArray (schema, code, name, externalSchema) { | ||
code += ` | ||
function ${name} (obj) { | ||
var json = '[' | ||
` | ||
|
||
var laterCode = '' | ||
|
||
const result = nested(laterCode, name, '[i]', schema.items) | ||
const result = nested(laterCode, name, '[i]', schema.items, externalSchema) | ||
|
||
code += ` | ||
const l = obj.length | ||
|
@@ -342,7 +371,7 @@ function buildArray (schema, code, name) { | |
return code | ||
} | ||
|
||
function nested (laterCode, name, key, schema) { | ||
function nested (laterCode, name, key, schema, externalSchema) { | ||
var code = '' | ||
var funcName | ||
const type = schema.type | ||
|
@@ -370,14 +399,14 @@ function nested (laterCode, name, key, schema) { | |
break | ||
case 'object': | ||
funcName = (name + key).replace(/[-.\[\]]/g, '') | ||
laterCode = buildObject(schema, laterCode, funcName) | ||
laterCode = buildObject(schema, laterCode, funcName, externalSchema) | ||
code += ` | ||
json += ${funcName}(obj${key}) | ||
` | ||
break | ||
case 'array': | ||
funcName = (name + key).replace(/[-.\[\]]/g, '') | ||
laterCode = buildArray(schema, laterCode, funcName) | ||
laterCode = buildArray(schema, laterCode, funcName, externalSchema) | ||
code += ` | ||
json += ${funcName}(obj${key}) | ||
` | ||
|
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" | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.