Skip to content
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
5 changes: 5 additions & 0 deletions src/generateClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ test('generateClientSource: string enum exports emit const object and derived ty
} as const;`),
true,
);
assert.equal(
generated.includes('issueStatus: p.union([p.literal("open"), p.literal("closed"), p.literal("ignored")], \'invalid\'),'),
true,
'string enum exports must use runtyp 2 p.union([...], message) syntax',
);
assert.equal(
generated.includes('export type IssueStatus = (typeof IssueStatus)[keyof typeof IssueStatus];'),
true,
Expand Down
16 changes: 15 additions & 1 deletion src/generateValidators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,21 @@ test('schemaToRuntyp: union via oneOf', (assert) => {
],
});

assert.equal(expr, 'p.union(p.string(), p.number())');
assert.equal(expr, 'p.union([p.string(), p.number()], \'invalid\')');

});

test('schemaToRuntyp: string enum uses runtyp 2 union syntax', (assert) => {

const expr = schemaToRuntyp({
type: 'string',
enum: ['open', 'closed'],
});

assert.equal(
expr,
'p.union([p.literal("open"), p.literal("closed")], \'invalid\')',
);

});

Expand Down
12 changes: 9 additions & 3 deletions src/generateValidators/schemaToRuntyp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ function escapeString(value: string): string {

}

function unionExpr(parts: string[]): string {

return `p.union([${parts.join(', ')}], 'invalid')`;

}

function schemaToRuntypExpr(schema: unknown): string {

if (!isRecord(schema)) {
Expand All @@ -32,7 +38,7 @@ function schemaToRuntypExpr(schema: unknown): string {

if (literals.length === 1) return literals[0];

return `p.union(${literals.join(', ')})`;
return unionExpr(literals);

}

Expand All @@ -48,7 +54,7 @@ function schemaToRuntypExpr(schema: unknown): string {

if (parts.length === 1) return parts[0];

return `p.union(${parts.join(', ')})`;
return unionExpr(parts);

}

Expand All @@ -58,7 +64,7 @@ function schemaToRuntypExpr(schema: unknown): string {

if (parts.length === 1) return parts[0];

return `p.union(${parts.join(', ')})`;
return unionExpr(parts);

}

Expand Down
Loading