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

feat: implement ZodSet visitor and add set type support #224

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/lib/primitives/zod/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,13 @@ export const zodVisitor: {
ZodMap: () => {
throw new Error('Function not implemented.')
},
ZodSet: () => {
throw new Error('Function not implemented.')
ZodSet: (node, ctx) => {
const value = $array(ctx.render(node._def.valueType), {
minItems: node._def.minSize?.value,
maxItems: node._def.maxSize?.value,
set: true,
})
return value
},
ZodFunction: () => {
throw new Error('Function not implemented.')
Expand Down
10 changes: 8 additions & 2 deletions src/lib/visitor/arbitrary/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,21 @@ export const arbitraryVisitor: ThereforeVisitor<Arbitrary<unknown>, ArbitraryCon
}
return composite
},
array: ({ _children: [items], _options: options }, context) => {
array: ({ _children: [items], _options: options, _origin: { zod } }, context) => {
const { minLength, maxLength, ...restArbitrary } = options.arbitrary ?? {}
const child = context.arbitrary(items)
if (options.set === true) {
return set(child, {
const value = set(child, {
minLength: options.minItems ?? minLength,
maxLength: options.maxItems ?? maxLength,
...restArbitrary,
})

if (zod !== undefined && (zod as ZodFirstPartySchemaTypes)._def.typeName === 'ZodSet') {
return value.map((x) => new Set(x))
}

return value
}
return array(child, {
minLength: options.minItems ?? minLength,
Expand Down
Loading
Loading