Skip to content

Commit c951bf2

Browse files
committed
WIP: label accidentally added to default
1 parent e96f29b commit c951bf2

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/lib/jsonSchema/schemaInfo.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,21 @@ export function schemaInfo(
5757
) as JSONSchema7[])
5858
: undefined;
5959

60-
const properties =
61-
schema.properties && types.includes('object')
62-
? (Object.fromEntries(
63-
Object.entries(schema.properties).filter(([, value]) => typeof value !== 'boolean')
64-
) as { [key: string]: JSONSchema7 })
60+
const additionalProperties =
61+
types.includes('object') && typeof schema.additionalProperties == 'object'
62+
? schemaInfo(schema.additionalProperties, isOptional, path)
6563
: undefined;
6664

65+
const mergedProperties = types.includes('object')
66+
? { ...additionalProperties?.properties, ...schema.properties }
67+
: undefined;
68+
69+
const properties = mergedProperties
70+
? (Object.fromEntries(
71+
Object.entries(mergedProperties).filter(([, value]) => typeof value !== 'boolean')
72+
) as { [key: string]: JSONSchema7 })
73+
: undefined;
74+
6775
const union = unionInfo(schema)?.filter((u) => u.type !== 'null' && u.const !== null);
6876

6977
return {

src/tests/superValidate.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,39 @@ describe('Zod', () => {
628628
expect(() => zod(schema)).toThrow(Error);
629629
});
630630

631+
describe.only('with z.record', () => {
632+
it('should work with additionalProperties for records', async () => {
633+
const schema = z.object({
634+
id: z.string(),
635+
options: z.record(
636+
z.string(),
637+
z.object({
638+
label: z.string().refine((value) => value.length > 0, {
639+
message: 'Label is required'
640+
})
641+
})
642+
)
643+
});
644+
645+
console.dir(zod(schema).jsonSchema, { depth: 10 }); //debug
646+
647+
let row = {
648+
id: '1',
649+
options: {
650+
'1': {
651+
label: 'Option 1'
652+
},
653+
'2': {
654+
label: ''
655+
}
656+
}
657+
};
658+
659+
const form = await superValidate(row, zod(schema));
660+
console.dir(form, { depth: 10 }); //debug
661+
});
662+
});
663+
631664
schemaTest(zod(schema));
632665
});
633666

0 commit comments

Comments
 (0)