zod#ZodStringDef TypeScript Examples

The following examples show how to use zod#ZodStringDef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: string.ts    From zod-to-json-schema with ISC License 5 votes vote down vote up
export function parseStringDef(def: ZodStringDef): JsonSchema7StringType {
  const res: JsonSchema7StringType = {
    type: "string",
  };

  if (def.checks) {
    for (const check of def.checks) {
      switch (check.kind) {
        case "min":
          res.minLength = check.value;
          break;
        case "max":
          res.maxLength = check.value;
          break;
        case "email":
          res.format = "email";
          break;
        case "url":
          res.format = "uri";
          break;
        case "uuid":
          res.format = "uuid";
          break;
        case "regex":
          res.pattern = check.regex.source;
          break;
        case "cuid":
          res.pattern = "^c[^\\s-]{8,}$";
          break;
        case "trim":
          // I have no idea why this is a check in Zod. It's a runtime string manipulation method.
          break;
        default:
          ((_: never) => {})(check);
      }
    }
  }

  return res;
}