values.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import type {CodeKeywordDefinition} from "../../types"
  2. import type KeywordCxt from "../../compile/context"
  3. import {Type} from "../../compile/subschema"
  4. import {alwaysValidSchema} from "../../compile/util"
  5. import {not, Name} from "../../compile/codegen"
  6. import {checkMetadata} from "./metadata"
  7. import {checkNullableObject} from "./nullable"
  8. const def: CodeKeywordDefinition = {
  9. keyword: "values",
  10. schemaType: "object",
  11. code(cxt: KeywordCxt) {
  12. checkMetadata(cxt)
  13. const {gen, data, schema, it} = cxt
  14. if (alwaysValidSchema(it, schema)) return
  15. const [valid, cond] = checkNullableObject(cxt, data)
  16. gen.if(cond, () => gen.assign(valid, validateMap()))
  17. cxt.pass(valid)
  18. function validateMap(): Name | boolean {
  19. const _valid = gen.name("valid")
  20. if (it.allErrors) {
  21. const validMap = gen.let("valid", true)
  22. validateValues(() => gen.assign(validMap, false))
  23. return validMap
  24. }
  25. gen.var(_valid, true)
  26. validateValues(() => gen.break())
  27. return _valid
  28. function validateValues(notValid: () => void): void {
  29. gen.forIn("key", data, (key) => {
  30. cxt.subschema(
  31. {
  32. keyword: "values",
  33. dataProp: key,
  34. dataPropType: Type.Str,
  35. },
  36. _valid
  37. )
  38. gen.if(not(_valid), notValid)
  39. })
  40. }
  41. }
  42. },
  43. }
  44. export default def