Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support readonly() #90

Closed
thorhj opened this issue Sep 26, 2023 · 5 comments
Closed

Support readonly() #90

thorhj opened this issue Sep 26, 2023 · 5 comments

Comments

@thorhj
Copy link

thorhj commented Sep 26, 2023

With zod 3.22.0+ we now have .readonly() which can be applied to various types, most notably array and object. This is not supported in zod-to-json-schema yet, so it just returns an empty schema:

import { z } from "zod";
import zodToJsonSchema from "zod-to-json-schema";

const schema = z.object({
  foo: z.boolean(),
  bar: z.number(),
});

console.log("Regular types:");
console.log(zodToJsonSchema(schema));
console.log(zodToJsonSchema(schema.array()));

console.log();
console.log("Readonly types:");
console.log(zodToJsonSchema(schema.readonly()));
console.log(zodToJsonSchema(schema.array().readonly()));
Regular types:
{
  type: 'object',
  properties: { foo: { type: 'boolean' }, bar: { type: 'number' } },
  required: [ 'foo', 'bar' ],
  additionalProperties: false,
  '$schema': 'http://json-schema.org/draft-07/schema#'
}
{
  type: 'array',
  items: {
    type: 'object',
    properties: { foo: [Object], bar: [Object] },
    required: [ 'foo', 'bar' ],
    additionalProperties: false
  },
  '$schema': 'http://json-schema.org/draft-07/schema#'
}

Readonly types:
{ '$schema': 'http://json-schema.org/draft-07/schema#' }
{ '$schema': 'http://json-schema.org/draft-07/schema#' }
@kachkaev
Copy link

kachkaev commented Oct 12, 2023

Same issue here. Does readonly() affect the result or is it a ‘transparent’ definition , like .catch()?

import { ZodCatchDef } from "zod";
import { parseDef } from "../parseDef";
import { Refs } from "../Refs";
export const parseCatchDef = (def: ZodCatchDef<any>, refs: Refs) => {
return parseDef(def.innerType._def, refs);
};

If it’s the latter, supporting .readonly() should be pretty straightforward. Happy to submit a PR if it makes sense.


UPD: This local patch seems to be working – I no longer get empty schemas 🎉

diff --git a/src/parseDef.js b/src/parseDef.js
index 4f33785322590999d31593cdcbdb174175930799..f8a09be4387cd64c1534f1f3a2a6c70f4625e1f2 100644
--- a/src/parseDef.js
+++ b/src/parseDef.js
@@ -151,6 +151,8 @@ const selectParser = (def, typeName, refs) => {
         case zod_1.ZodFirstPartyTypeKind.ZodBranded:
             return (0, branded_1.parseBrandedDef)(def, refs);
         case zod_1.ZodFirstPartyTypeKind.ZodCatch:
+        // https://github.com/StefanTerdell/zod-to-json-schema/issues/90
+        case zod_1.ZodFirstPartyTypeKind.ZodReadonly:
             return (0, catch_1.parseCatchDef)(def, refs);
         case zod_1.ZodFirstPartyTypeKind.ZodPipeline:
             return (0, pipeline_1.parsePipelineDef)(def, refs);

For simplicity, ZodReadonly is processed the same way as ZodCatch.

@StefanTerdell
Copy link
Owner

Done in master

@fredrikj31
Copy link

When is this gonna be released @StefanTerdell ?

@StefanTerdell
Copy link
Owner

@fredrikj31 about a minute ago

@kachkaev
Copy link

Great, thanks @StefanTerdell! The new version works like a charm ✨

If you have a bit of time, can you please update the list of releases?

It should help Renovate (and other similar tools) show the list changes in autogenerated PRs. It is empty at the moment:

Screenshot 2023-11-18 at 11 58 20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants