Z
Zod17mo ago
Steve

Scott Trinh Would you be able to help

@Scott Trinh Would you be able to help me out? I have some complex schema:
export const PublicatietoolRootElementSchema = z.union([
PublicatietoolElementHeadingSchema,
PublicatietoolElementImageSchema,
PublicatietoolElementListOrderedSchema,
PublicatietoolElementListUnorderedSchema,
PublicatietoolElementQuotationSchema,
PublicatietoolElementTableSchema,
PublicatietoolElementTextSchema,
]);
export const PublicatietoolRootElementSchema = z.union([
PublicatietoolElementHeadingSchema,
PublicatietoolElementImageSchema,
PublicatietoolElementListOrderedSchema,
PublicatietoolElementListUnorderedSchema,
PublicatietoolElementQuotationSchema,
PublicatietoolElementTableSchema,
PublicatietoolElementTextSchema,
]);
But the .Union doesn't work, most of them have overlapping fields, and they have one discriminator: .type
3 Replies
Steve
SteveOP17mo ago
Steve
SteveOP17mo ago
import { describe, test } from '@jest/globals';
import { SchemaMatcher } from '../__testUtils__/zod/SchemaMatcher.js';
import { PublicatietoolRootElementSchema } from '../../src/types/publicatietool/PublicatietoolRootElement.js';
import { PublicatietoolElementQuotation } from '../../src/types/publicatietool/elements/PublicatietoolElementQuotation.js';
import { PublicatietoolElementTable } from '../../src/types/publicatietool/elements/PublicatietoolElementTable.js';

describe('PublicatietoolRootElement', () => {
const assertSchema = new SchemaMatcher(PublicatietoolRootElementSchema);

test(`Schema allows values`, async () => {
assertSchema
.parsing({
type: 'quotation',
content: 'Some content',
attributes: {},
validation: [],
})
.toSucceed()
.andEqual(new PublicatietoolElementQuotation('Some content'));
});

test('Valid table', async () => {
const html =
'<table><thead>' +
'<tr><th>Test</th><th>Test2</th></tr></thead>' +
'<tbody><tr><td><a href="#"></a><p></p><p>Test</p></td>' +
'<td><p>Test</p></td></tr></tbody></table>';

assertSchema
.parsing({
type: 'table',
content: html,
attributes: {},
validation: [],
})
.toSucceed()
.andEqual(new PublicatietoolElementTable(html));
});
});
import { describe, test } from '@jest/globals';
import { SchemaMatcher } from '../__testUtils__/zod/SchemaMatcher.js';
import { PublicatietoolRootElementSchema } from '../../src/types/publicatietool/PublicatietoolRootElement.js';
import { PublicatietoolElementQuotation } from '../../src/types/publicatietool/elements/PublicatietoolElementQuotation.js';
import { PublicatietoolElementTable } from '../../src/types/publicatietool/elements/PublicatietoolElementTable.js';

describe('PublicatietoolRootElement', () => {
const assertSchema = new SchemaMatcher(PublicatietoolRootElementSchema);

test(`Schema allows values`, async () => {
assertSchema
.parsing({
type: 'quotation',
content: 'Some content',
attributes: {},
validation: [],
})
.toSucceed()
.andEqual(new PublicatietoolElementQuotation('Some content'));
});

test('Valid table', async () => {
const html =
'<table><thead>' +
'<tr><th>Test</th><th>Test2</th></tr></thead>' +
'<tbody><tr><td><a href="#"></a><p></p><p>Test</p></td>' +
'<td><p>Test</p></td></tr></tbody></table>';

assertSchema
.parsing({
type: 'table',
content: html,
attributes: {},
validation: [],
})
.toSucceed()
.andEqual(new PublicatietoolElementTable(html));
});
});
So the second test succeeds, the first does not Wait, this might be my own mistake
Steve
SteveOP17mo ago
@Scott Trinh Thanks for all your help. You are the best rubber duck!
No description
Want results from more Discord servers?
Add your server