-
-
Notifications
You must be signed in to change notification settings - Fork 209
const is validated by fjs, add strict mode #579
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9c2d6fb
const is validated by fjs
Uzlopak f00533a
fix linting error
Uzlopak a7c9ac7
add const validator
Uzlopak 2c72599
reduce complexity
Uzlopak 911397a
encapsulate keyword const
Uzlopak 38ea0f3
Merge branch 'master' into const-is-validated
Uzlopak 14731a6
fix merge error
Uzlopak c0be2b1
small reorder
Uzlopak 97ca437
Merge branch 'master' into const-is-validated
Uzlopak 8c8115d
add strict mode
Uzlopak 655d3c5
no need to validate if strict is false
Uzlopak 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 hidden or 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 hidden or 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,141 @@ | ||
'use strict' | ||
|
||
function validator (input, accessPath = 'value', mode = 'function') { | ||
if (mode === 'integration') { | ||
return `${_const(input, accessPath)} true\n` | ||
} else { | ||
return new Function('value', `return (\n${_const(input, accessPath)} true\n)`) // eslint-disable-line no-new-func | ||
} | ||
} | ||
|
||
function _const (input, accessPath) { | ||
let functionCode = '' | ||
switch (typeof input) { | ||
case 'undefined': | ||
functionCode += _constUndefined(input, accessPath) | ||
break | ||
case 'bigint': | ||
functionCode += _constBigInt(input, accessPath) | ||
break | ||
case 'boolean': | ||
functionCode += _constBoolean(input, accessPath) | ||
break | ||
case 'number': | ||
functionCode += _constNumber(input, accessPath) | ||
break | ||
case 'string': | ||
functionCode += _constString(input, accessPath) | ||
break | ||
case 'object': | ||
functionCode += _constObject(input, accessPath) | ||
break | ||
} | ||
return functionCode | ||
} | ||
|
||
function _constUndefined (input, accessPath) { | ||
return `typeof ${accessPath} === 'undefined' &&\n` | ||
} | ||
|
||
function _constBigInt (input, accessPath) { | ||
return `typeof ${accessPath} === 'bigint' && ${accessPath} === ${input.toString()}n &&\n` | ||
} | ||
|
||
function _constNumber (input, accessPath) { | ||
if (input !== input) { // eslint-disable-line no-self-compare | ||
return `typeof ${accessPath} === 'number' && ${accessPath} !== ${accessPath} &&\n` | ||
} else { | ||
return `typeof ${accessPath} === 'number' && ${accessPath} === ${input.toString()} &&\n` | ||
} | ||
} | ||
|
||
function _constBoolean (input, accessPath) { | ||
return `typeof ${accessPath} === 'boolean' && ${accessPath} === ${input ? 'true' : 'false'} &&\n` | ||
} | ||
|
||
function _constString (input, accessPath) { | ||
return `typeof ${accessPath} === 'string' && ${accessPath} === ${JSON.stringify(input)} &&\n` | ||
} | ||
|
||
function _constNull (input, accessPath) { | ||
return `typeof ${accessPath} === 'object' && ${accessPath} === null &&\n` | ||
} | ||
|
||
function _constArray (input, accessPath) { | ||
let functionCode = `Array.isArray(${accessPath}) && ${accessPath}.length === ${input.length} &&\n` | ||
for (let i = 0; i < input.length; ++i) { | ||
functionCode += _const(input[i], `${accessPath}[${i}]`) | ||
} | ||
return functionCode | ||
} | ||
|
||
function _constPOJO (input, accessPath) { | ||
const keys = Object.keys(input) | ||
|
||
let functionCode = `typeof ${accessPath} === 'object' && ${accessPath} !== null &&\n` | ||
|
||
functionCode += `Object.keys(${accessPath}).length === ${keys.length} &&\n` | ||
|
||
if (typeof input.valueOf === 'function') { | ||
functionCode += `(${accessPath}.valueOf === Object.prototype.valueOf || ${accessPath}.valueOf() === ${JSON.stringify(input.valueOf())}) &&\n` | ||
} | ||
|
||
if (typeof input.toString === 'function') { | ||
functionCode += `(${accessPath}.toString === Object.prototype.toString || ${accessPath}.toString() === ${JSON.stringify(input.toString())}) &&\n` | ||
} | ||
// check keys | ||
for (const key of keys) { | ||
functionCode += `${JSON.stringify(key)} in ${accessPath} &&\n` | ||
} | ||
// check values | ||
for (const key of keys) { | ||
functionCode += `${_const(input[key], `${accessPath}[${JSON.stringify(key)}]`)}` | ||
} | ||
|
||
return functionCode | ||
} | ||
|
||
function _constRegExp (input, accessPath) { | ||
return `typeof ${accessPath} === 'object' && ${accessPath} !== null && ${accessPath}.constructor === RegExp && ${accessPath}.source === ${JSON.stringify(input.source)} && ${accessPath}.flags === ${JSON.stringify(input.flags)} &&\n` | ||
} | ||
|
||
function _constDate (input, accessPath) { | ||
return `typeof ${accessPath} === 'object' && ${accessPath} !== null && ${accessPath}.constructor === Date && ${accessPath}.getTime() === ${input.getTime()} &&\n` | ||
} | ||
|
||
function _constObject (input, accessPath) { | ||
if (input === null) { | ||
return _constNull(input, accessPath) | ||
} else if (Array.isArray(input)) { | ||
return _constArray(input, accessPath) | ||
} else if (input.constructor === RegExp) { | ||
return _constRegExp(input, accessPath) | ||
} else if (input.constructor === Date) { | ||
return _constDate(input, accessPath) | ||
} else { | ||
return _constPOJO(input, accessPath) | ||
} | ||
} | ||
|
||
function keyword (context, location, input) { | ||
const schema = location.schema | ||
let schemaRef = location.getSchemaRef() | ||
if (schemaRef.startsWith(context.rootSchemaId)) { | ||
schemaRef = schemaRef.replace(context.rootSchemaId, '') | ||
} | ||
|
||
if (context.strict) { | ||
return ` | ||
if (${validator(schema.const, input, 'integration')}) { | ||
json += ${JSON.stringify(JSON.stringify(schema.const))} | ||
} else { | ||
throw new Error(\`The value of '${schemaRef}' does not match schema definition.\`) | ||
} | ||
` | ||
} else { | ||
return `json += JSON.stringify(${input})` | ||
} | ||
} | ||
|
||
module.exports.validator = validator | ||
module.exports.keyword = keyword |
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.
Uh oh!
There was an error while loading. Please reload this page.