Skip to content

Commit

Permalink
refactor: pass valid var name to applySubschema
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 19, 2020
1 parent f27dd0f commit 77b6d00
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 39 deletions.
2 changes: 0 additions & 2 deletions lib/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import CodeGen from "./codegen"
import {toQuotedString} from "./util"
import {MissingRefError} from "./error_classes"
import validateCode from "./validate"
import {applySchema} from "./subschema"
import {Rule} from "./rules"
import {CompilationContext, KeywordDefinition, ErrorObject} from "../types"

Expand Down Expand Up @@ -113,7 +112,6 @@ function compile(schema, root, localRefs, baseId) {
MissingRefError,
RULES,
validateCode,
applySchema, // TODO remove to imports
util, // TODO remove to imports
resolve, // TODO remove to imports
resolveRef, // TODO remove to imports
Expand Down
22 changes: 12 additions & 10 deletions lib/compile/subschema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ export interface SubschemaContext {
compositeRule?: true
}

export function applySchema(it: CompilationContext, subschema: SubschemaContext): string {
const {gen, level} = it
const nextContext = {...it, ...subschema, level: level + 1}
const nextValid = gen.name("valid")
// TODO remove true once appendGen is removed
validateCode(nextContext, nextValid, true)
return nextValid
export function applySchema(
it: CompilationContext,
subschema: SubschemaContext,
valid: string
): void {
const nextContext = {...it, ...subschema, level: it.level + 1}
// TODO remove "true" once appendGen is removed
validateCode(nextContext, valid, true)
}

export enum Expr {
Expand All @@ -37,8 +38,9 @@ interface SubschemaApplication {

export function applySubschema(
it: CompilationContext,
{keyword, schemaProp, compositeRule, dataProp, expr}: SubschemaApplication
): string {
{keyword, schemaProp, compositeRule, dataProp, expr}: SubschemaApplication,
valid: string
): void {
const schema = it.schema[keyword]
const subschema: SubschemaContext =
schemaProp === undefined
Expand Down Expand Up @@ -71,5 +73,5 @@ export function applySubschema(
}

if (compositeRule) subschema.compositeRule = compositeRule
return applySchema(it, subschema)
applySchema(it, subschema, valid)
}
2 changes: 0 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Cache from "./cache"
import CodeGen from "./compile/codegen"
import {ValidationRules, Rule} from "./compile/rules"
import {MissingRefError} from "./compile/error_classes"
import {SubschemaContext} from "./compile/subschema"

export interface Options {
$data?: boolean
Expand Down Expand Up @@ -126,7 +125,6 @@ export interface CompilationContext {
// }
compositeRule?: boolean
validateCode: (it: CompilationContext) => string | void // TODO remove string
applySchema: (it: CompilationContext, subSchema: SubschemaContext) => string
usePattern: (str: string) => string
useDefault: (value: any) => string
useCustomRule: (rule: Rule, schema: any, parentSchema: object, it: CompilationContext) => any
Expand Down
3 changes: 2 additions & 1 deletion lib/vocabularies/applicator/allOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const def: KeywordDefinition = {
const {opts} = it
let emptySchemas = true
let closeBlocks = ""
const valid = gen.name("valid")
schema.forEach((sch: object | boolean, i: number) => {
if (nonEmptySchema(it, sch)) {
emptySchemas = false
const valid = applySubschema(it, {keyword: "allOf", schemaProp: i})
applySubschema(it, {keyword: "allOf", schemaProp: i}, valid)
if (!opts.allErrors) {
gen.code(`if (${valid}) {`)
closeBlocks += "}"
Expand Down
15 changes: 10 additions & 5 deletions lib/vocabularies/applicator/anyOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ const def: KeywordDefinition = {
)

let closeBlocks = ""
const schValid = gen.name("valid")
schema.forEach((_, i: number) => {
const schValid = applySubschema(it, {
keyword: "anyOf",
schemaProp: i,
compositeRule: true,
})
applySubschema(
it,
{
keyword: "anyOf",
schemaProp: i,
compositeRule: true,
},
schValid
)
gen.code(
`${valid} = ${valid} || ${schValid};
if (!${valid}) {`
Expand Down
21 changes: 13 additions & 8 deletions lib/vocabularies/applicator/contains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@ const def: KeywordDefinition = {
gen.code(`const ${errsCount} = errors;`)

if (nonEmptySchema(it, schema)) {
const valid = gen.name("valid")
const i = gen.name("i")
gen.code(`for (let ${i}=0; ${i}<${data}.length; ${i}++) {`)
const schValid = applySubschema(it, {
keyword: "contains",
dataProp: i,
expr: Expr.Num,
compositeRule: true,
})
applySubschema(
it,
{
keyword: "contains",
dataProp: i,
expr: Expr.Num,
compositeRule: true,
},
valid
)
gen.code(
`if (${schValid}) break;
`if (${valid}) break;
}`
)

// TODO refactor failCompoundOrReset? It is different from anyOf though
gen.code(`if (!${schValid}) {`)
gen.code(`if (!${valid}) {`)
reportError(cxt, def.error as KeywordErrorDefinition)
gen.code(`} else {`)
resetErrorsCount(gen, errsCount)
Expand Down
26 changes: 15 additions & 11 deletions lib/vocabularies/applicator/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,23 @@ const def: KeywordDefinition = {
}

function validateDefinedItems(): void {
const valid = gen.name("valid")
schema.forEach((sch: any, i: number) => {
if (nonEmptySchema(it, sch)) {
gen.code(`if (${len} > ${i}) {`)
const schValid = applySubschema(it, {
keyword: "items",
schemaProp: i,
dataProp: i,
expr: Expr.Const,
})
applySubschema(
it,
{
keyword: "items",
schemaProp: i,
dataProp: i,
expr: Expr.Const,
},
valid
)
gen.code(`}`)
if (!it.opts.allErrors) {
gen.code(`if (${schValid}) {`)
gen.code(`if (${valid}) {`)
closeBlocks += "}"
}
}
Expand All @@ -65,11 +70,10 @@ const def: KeywordDefinition = {
function validateItems(keyword: string, startFrom: number): void {
const i = gen.name("i")
gen.code(`for (let ${i}=${startFrom}; ${i}<${len}; ${i}++) {`)
const schValid = applySubschema(it, {keyword, dataProp: i, expr: Expr.Num})
const valid = gen.name("valid")
applySubschema(it, {keyword, dataProp: i, expr: Expr.Num}, valid)
if (!it.opts.allErrors) {
gen.code(`if (!${schValid}) {
break;
}`)
gen.code(`if (!${valid}) break;`)
}
gen.code("}")
}
Expand Down

0 comments on commit 77b6d00

Please sign in to comment.