Skip to content

Commit

Permalink
refactor: contains to typescript (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 19, 2020
1 parent 4515090 commit f05a6c0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/dot/contains.jst
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ var {{=$valid}};
{{= $closingBraces }}

if (!{{=$nextValid}}) {
{{??}}
if ({{=$data}}.length == 0) {
{{?}}

{{# def.error:'contains' }}
} else {
{{? $nonEmptySchema }}
{{# def.resetErrors }}
{{?}}
{{# def.resetErrors }}
{{? it.opts.allErrors }} } {{?}}
{{??}}
if ({{=$data}}.length == 0) {
{{# def.error:'contains' }}
}
{{? !it.opts.allErrors }} else { {{?}}
{{?}}

{{ it.gen.code(out); }}
39 changes: 39 additions & 0 deletions lib/vocabularies/applicator/contains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {KeywordDefinition} from "../../types"
import {nonEmptySchema} from "../util"
import {applySubschema, Expr} from "../../compile/subschema"

const def: KeywordDefinition = {
keyword: "contains",
type: "array",
schemaType: ["object", "boolean"],
code(cxt) {
const {gen, fail, schema, data, it} = cxt
const {dataLevel} = it
let closeBlocks = ""
const errsCount = gen.name("_errs")
gen.code(`const ${errsCount} = errors;`)

if (nonEmptySchema(it, schema)) {
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,
})
gen.code(
`if (${schValid}) break;
}`
)
} else {
fail(`${data}.length === 0`)
}
},
error: {
message: ({schema}) => `"should NOT have more than ${schema.length} items"`,
params: ({schema}) => `{limit: ${schema.length}}`,
},
}

module.exports = def

0 comments on commit f05a6c0

Please sign in to comment.