limitLength.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const codegen_1 = require("../../compile/codegen");
  4. const ucs2length_1 = require("../../compile/ucs2length");
  5. const error = {
  6. message({ keyword, schemaCode }) {
  7. const comp = keyword === "maxLength" ? "more" : "fewer";
  8. return codegen_1.str `should NOT have ${comp} than ${schemaCode} characters`;
  9. },
  10. params: ({ schemaCode }) => codegen_1._ `{limit: ${schemaCode}}`,
  11. };
  12. const def = {
  13. keyword: ["maxLength", "minLength"],
  14. type: "string",
  15. schemaType: "number",
  16. $data: true,
  17. error,
  18. code(cxt) {
  19. const { keyword, data, schemaCode, it } = cxt;
  20. const op = keyword === "maxLength" ? codegen_1.operators.GT : codegen_1.operators.LT;
  21. let len;
  22. if (it.opts.unicode === false) {
  23. len = codegen_1._ `${data}.length`;
  24. }
  25. else {
  26. const u2l = cxt.gen.scopeValue("func", {
  27. ref: ucs2length_1.default,
  28. code: codegen_1._ `require("ajv/dist/compile/ucs2length").default`,
  29. });
  30. len = codegen_1._ `${u2l}(${data})`;
  31. }
  32. cxt.fail$data(codegen_1._ `${len} ${op} ${schemaCode}`);
  33. },
  34. };
  35. exports.default = def;
  36. //# sourceMappingURL=limitLength.js.map