UNPKG

1.25 kBPlain TextView Raw
1import type {
2 CodeKeywordDefinition,
3 ErrorObject,
4 KeywordErrorDefinition,
5 AnySchema,
6} from "../../types"
7import type {KeywordCxt} from "../../compile/validate"
8import {_, not} from "../../compile/codegen"
9import {alwaysValidSchema} from "../../compile/util"
10
11export type PropertyNamesError = ErrorObject<"propertyNames", {propertyName: string}, AnySchema>
12
13const error: KeywordErrorDefinition = {
14 message: "property name must be valid",
15 params: ({params}) => _`{propertyName: ${params.propertyName}}`,
16}
17
18const def: CodeKeywordDefinition = {
19 keyword: "propertyNames",
20 type: "object",
21 schemaType: ["object", "boolean"],
22 error,
23 code(cxt: KeywordCxt) {
24 const {gen, schema, data, it} = cxt
25 if (alwaysValidSchema(it, schema)) return
26 const valid = gen.name("valid")
27
28 gen.forIn("key", data, (key) => {
29 cxt.setParams({propertyName: key})
30 cxt.subschema(
31 {
32 keyword: "propertyNames",
33 data: key,
34 dataTypes: ["string"],
35 propertyName: key,
36 compositeRule: true,
37 },
38 valid
39 )
40 gen.if(not(valid), () => {
41 cxt.error(true)
42 if (!it.allErrors) gen.break()
43 })
44 })
45
46 cxt.ok(valid)
47 },
48}
49
50export default def