diff --git a/package.json b/package.json index ae06083..45ca335 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/MockGenerator.ts b/src/MockGenerator.ts index 7bcf81d..85795f0 100644 --- a/src/MockGenerator.ts +++ b/src/MockGenerator.ts @@ -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(); @@ -78,6 +79,7 @@ export default class MockGenerator { ZodBranded: BrandedGenerator, ZodFunction: FunctionGenerator, ZodLazy: LazyGenerator, + ZodReadonly: ReadonlyGenerator, }; if (this.schema._def.typeName in generatorMap) { diff --git a/src/generators/ReadonlyGenerator.ts b/src/generators/ReadonlyGenerator.ts new file mode 100644 index 0000000..26c10ac --- /dev/null +++ b/src/generators/ReadonlyGenerator.ts @@ -0,0 +1,10 @@ +import type { ZodReadonly, ZodTypeAny } from 'zod'; +import MockGenerator from '../MockGenerator'; +import type BaseGenerator from './BaseGenerator'; + +export default class ReadonlyGenerator> implements BaseGenerator { + public generate(schema: U) { + const mockGenerator = new MockGenerator(schema._def.innerType); + return Object.freeze(mockGenerator.generate()); + } +} diff --git a/tests/readonly.test.ts b/tests/readonly.test.ts new file mode 100644 index 0000000..eb24e09 --- /dev/null +++ b/tests/readonly.test.ts @@ -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); + }); +}); diff --git a/yarn.lock b/yarn.lock index f82919a..3f097bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==