-
-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(validate): keywords validation to typescript (WIP)
- Loading branch information
1 parent
822336c
commit 6c48299
Showing
5 changed files
with
142 additions
and
9 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import {CompilationContext} from "../../types" | ||
import {shouldUseGroup, shouldUseRule} from "./applicability" | ||
import {checkDataType, schemaHasRulesExcept} from "../util" | ||
import {assignDefaults} from "./defaults" | ||
import {reportTypeError} from "./dataType" | ||
|
||
export default function schemaKeywords( | ||
it: CompilationContext, | ||
types: string[], | ||
typeErrors: boolean, | ||
top: boolean | ||
) { | ||
const { | ||
gen, | ||
schema, | ||
level, | ||
dataLevel, | ||
RULES, | ||
opts: {allErrors, strictNumbers, useDefaults}, | ||
} = it | ||
let closingBraces2 = "" | ||
if (schema.$ref && !schemaHasRulesExcept(schema, RULES.all, "$ref")) { | ||
gen.code(RULES.all.$ref.code(it, "$ref")) | ||
if (!allErrors) { | ||
// TODO refactor with below | ||
const errCount = top ? "0" : `errs_${level}` | ||
gen.code(`if (errors === ${errCount}) {`) | ||
closingBraces2 += "}" | ||
} | ||
} else { | ||
for (const group of RULES) { | ||
if (shouldUseGroup(schema, group)) { | ||
if (group.type) { | ||
// TODO refactor `data${dataLevel || ""}` | ||
const checkType = checkDataType(group.type, `data${dataLevel || ""}`, strictNumbers) | ||
gen.code(`if (${checkType}) {`) | ||
} | ||
if (useDefaults) assignDefaults(it, group) | ||
let closingBraces1 = "" | ||
for (const rule of group.rules) { | ||
if (shouldUseRule(schema, rule)) { | ||
const code = rule.code(it, rule.keyword, group.type) | ||
if (code) { | ||
gen.code(code) | ||
if (!allErrors) closingBraces1 += "}" | ||
} | ||
} | ||
} | ||
if (!allErrors) gen.code(closingBraces1) | ||
if (group.type) { | ||
gen.code("}") | ||
if (types.length === 1 && types[0] === group.type && typeErrors) { | ||
gen.code(`else {`) | ||
reportTypeError(it) | ||
gen.code(`}`) | ||
} | ||
} | ||
if (!allErrors) { | ||
const errCount = top ? "0" : `errs_${level}` | ||
gen.code(`if (errors === ${errCount}) {`) | ||
closingBraces2 += "}" | ||
} | ||
} | ||
} | ||
} | ||
if (!allErrors) gen.code(closingBraces2) | ||
} | ||
|
||
// {{? it.schema.$ref && !$refKeywords }} | ||
// {{= it.RULES.all.$ref.code(it, '$ref') }} | ||
// {{? $breakOnError }} | ||
// } | ||
// if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) { | ||
// {{ $closingBraces2 += '}'; }} | ||
// {{?}} | ||
// {{??}} | ||
// {{~ it.RULES:$rulesGroup }} | ||
// {{? $shouldUseGroup($rulesGroup) }} | ||
// {{? $rulesGroup.type }} | ||
// if ({{= it.util.checkDataType($rulesGroup.type, $data, it.opts.strictNumbers) }}) { | ||
// {{?}} | ||
// {{? it.opts.useDefaults }} | ||
// {{ | ||
// it.gen._out = ""; | ||
// it.assignDefaults(it, $rulesGroup); | ||
// }} | ||
// {{=it.gen._out}} | ||
// {{?}} | ||
// | ||
|
||
// {{~ $rulesGroup.rules:$rule }} | ||
// {{? $shouldUseRule($rule) }} | ||
// {{ var $code = $rule.code(it, $rule.keyword, $rulesGroup.type); }} | ||
// {{? $code }} | ||
// {{= $code }} | ||
// {{? $breakOnError }} | ||
// {{ $closingBraces1 += '}'; }} | ||
// {{?}} | ||
// {{?}} | ||
// {{?}} | ||
// {{~}} | ||
|
||
// {{? $breakOnError }} | ||
// {{= $closingBraces1 }} | ||
// {{ $closingBraces1 = ''; }} | ||
// {{?}} | ||
|
||
// {{? $rulesGroup.type }} | ||
// } | ||
// {{? $typeSchema && $typeSchema === $rulesGroup.type && !(it.opts.coerceTypes && it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema)) }} | ||
// else { | ||
// {{ | ||
// var $schemaPath = it.schemaPath + '.type' | ||
// , $errSchemaPath = it.errSchemaPath + '/type'; | ||
// }} | ||
// {{# def.error:'type' }} | ||
// } | ||
// {{?}} | ||
// {{?}} | ||
|
||
// {{? $breakOnError }} | ||
// if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) { | ||
// {{ $closingBraces2 += '}'; }} | ||
// {{?}} | ||
// {{?}} | ||
// {{~}} | ||
// {{?}} | ||
|
||
// {{? $breakOnError }} {{= $closingBraces2 }} {{?}} |
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