Skip to content

Commit

Permalink
feat: readonly support
Browse files Browse the repository at this point in the history
  • Loading branch information
ItMaga committed Feb 18, 2024
1 parent 6ec9438 commit 510f561
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"unbuild": "^2.0.0",
"vite": "^4.2.1",
"vitest": "^0.29.8",
"zod": "^3.21.4"
"zod": "^3.22.4"
},
"simple-git-hooks": {
"pre-commit": "yarn lint-staged"
Expand Down
2 changes: 2 additions & 0 deletions src/MockGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import PipelineGenerator from './generators/PipelineGenerator';
import BrandedGenerator from './generators/BrandedGenerator';
import FunctionGenerator from './generators/FunctionGenerator';
import LazyGenerator from './generators/LazyGenerator';
import ReadonlyGenerator from './generators/ReadonlyGenerator';
import { DepthLimitError } from './errors/DepthLimitError';

const _schemasCache = new WeakMap<z.ZodTypeAny, any>();
Expand Down Expand Up @@ -78,6 +79,7 @@ export default class MockGenerator<T extends z.ZodTypeAny> {
ZodBranded: BrandedGenerator,
ZodFunction: FunctionGenerator,
ZodLazy: LazyGenerator,
ZodReadonly: ReadonlyGenerator,
};

if (this.schema._def.typeName in generatorMap) {
Expand Down
10 changes: 10 additions & 0 deletions src/generators/ReadonlyGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { ZodReadonly, ZodTypeAny } from 'zod';
import MockGenerator from '../MockGenerator';
import type BaseGenerator from './BaseGenerator';

export default class ReadonlyGenerator<T extends ZodTypeAny, U extends ZodReadonly<T>> implements BaseGenerator<U> {
public generate(schema: U) {
const mockGenerator = new MockGenerator(schema._def.innerType);
return Object.freeze(mockGenerator.generate());
}
}
22 changes: 22 additions & 0 deletions tests/readonly.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, test } from 'vitest';
import { z } from 'zod';
import { expect } from './utils/expect';

describe('Readonly', () => {
test('base', () => {
const object = z.object({ foo: z.string(), bar: z.number() }).readonly();
expect(object);

const array = z.array(z.string()).readonly();
expect(array);

const string = z.string().readonly();
expect(string);

const number = z.number().readonly();
expect(number);

const boolean = z.boolean().readonly();
expect(boolean);
});
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4385,7 +4385,7 @@ yocto-queue@^1.0.0:
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==

zod@^3.21.4:
version "3.21.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.21.4.tgz#10882231d992519f0a10b5dd58a38c9dabbb64db"
integrity sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==
zod@^3.22.4:
version "3.22.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"
integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==

0 comments on commit 510f561

Please sign in to comment.