Skip to content

Commit

Permalink
Merge pull request #174 from asteasolutions/task/#173-add-support-for…
Browse files Browse the repository at this point in the history
…-readonly

#173 Added support for ZodReadonly
  • Loading branch information
AGalabov committed Oct 3, 2023
2 parents bc7d52f + b04fc53 commit b38449c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ The list of all supported types as of now is:
- including `.catchall` resulting in the respective `additionalProperties` schema
- `ZodOptional`
- `ZodPipeline`
- `ZodReadonly`
- `ZodRecord`
- `ZodString`
- adding `format` for `.datetime()`, `.uuid()`, `.email()` and `.url()` and `pattern` for `.regex()` is also supported
Expand All @@ -528,7 +529,7 @@ You can still register such schemas on your own by providing a `type` via the `.

1. `z.nullable(schema)` [does not generate a $ref for underlying registered schemas](https://github.com/asteasolutions/zod-to-openapi/issues/141).
- This is an implementation limitation.
- However you can simply use `schema.nullable()` which has the exact same effect `zod` wise but it is also fully supported on our end.
- However you can simply use `schema.nullable()` which has the exact same effect `zod` wise but it is also fully supported on our end.

## Technologies

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"ts-jest": "^29.0.3",
"typescript": "^5.2.2",
"yaml": "^2.2.2",
"zod": "^3.20.2"
"zod": "^3.22.0"
},
"author": "Astea Solutions <info@asteasolutions.com>",
"license": "MIT"
Expand Down
12 changes: 12 additions & 0 deletions spec/modifiers/readonly.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from 'zod';
import { expectSchema } from '../lib/helpers';

describe('readonly', () => {
it('supports readonly', () => {
expectSchema([z.string().readonly().openapi('ReadonlyString')], {
ReadonlyString: {
type: 'string',
},
});
});
});
1 change: 1 addition & 0 deletions src/lib/zod-is-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ZodTypes = {
ZodObject: z.AnyZodObject;
ZodOptional: z.ZodOptional<any>;
ZodPipeline: z.ZodPipeline<any, any>;
ZodReadonly: z.ZodReadonly<any>;
ZodRecord: z.ZodRecord;
ZodSchema: z.ZodSchema;
ZodString: z.ZodString;
Expand Down
2 changes: 1 addition & 1 deletion src/openapi-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ export class OpenAPIGenerator {
return this.unwrapChained(schema.unwrap());
}

if (isZodType(schema, 'ZodDefault')) {
if (isZodType(schema, 'ZodDefault') || isZodType(schema, 'ZodReadonly')) {
return this.unwrapChained(schema._def.innerType);
}

Expand Down

0 comments on commit b38449c

Please sign in to comment.