optional.js 623 B

123456789101112131415161718192021
  1. import { parseDef } from "../parseDef.js";
  2. import { parseAnyDef } from "./any.js";
  3. export const parseOptionalDef = (def, refs) => {
  4. if (refs.currentPath.toString() === refs.propertyPath?.toString()) {
  5. return parseDef(def.innerType._def, refs);
  6. }
  7. const innerSchema = parseDef(def.innerType._def, {
  8. ...refs,
  9. currentPath: [...refs.currentPath, "anyOf", "1"],
  10. });
  11. return innerSchema
  12. ? {
  13. anyOf: [
  14. {
  15. not: parseAnyDef(refs),
  16. },
  17. innerSchema,
  18. ],
  19. }
  20. : parseAnyDef(refs);
  21. };