Skip to content
Snippets Groups Projects
Unverified Commit 0777f549 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

feat(schema-utils): Support Toml format (#23930)

parent 24c49a7a
No related branches found
No related tags found
No related merge requests found
import { codeBlock } from 'common-tags';
import { z } from 'zod';
import {
Json,
Json5,
LooseArray,
LooseRecord,
Toml,
Url,
UtcDate,
Yaml,
......@@ -340,4 +342,55 @@ describe('util/schema-utils', () => {
});
});
});
describe('Toml', () => {
const Schema = Toml.pipe(
z.object({ foo: z.object({ bar: z.literal('baz') }) })
);
it('parses valid toml', () => {
const content = codeBlock`
[foo]
bar = "baz"
`;
expect(Schema.parse(content)).toEqual({
foo: { bar: 'baz' },
});
});
it('throws error for invalid schema', () => {
const content = codeBlock`
[foo]
bar = "brb"
`;
expect(Schema.safeParse(content)).toMatchObject({
error: {
issues: [
{
received: 'brb',
code: 'invalid_literal',
expected: 'baz',
path: ['foo', 'bar'],
},
],
},
success: false,
});
});
it('throws error for invalid toml', () => {
expect(Schema.safeParse('clearly_invalid')).toMatchObject({
error: {
issues: [
{
message: 'Invalid TOML',
code: 'custom',
path: [],
},
],
},
success: false,
});
});
});
});
import { JsonMap, parse } from '@iarna/toml';
import { load } from 'js-yaml';
import JSON5 from 'json5';
import { DateTime } from 'luxon';
......@@ -242,3 +243,12 @@ export const Yaml = z.string().transform((str, ctx): JsonValue => {
return z.NEVER;
}
});
export const Toml = z.string().transform((str, ctx): JsonMap => {
try {
return parse(str);
} catch (e) {
ctx.addIssue({ code: 'custom', message: 'Invalid TOML' });
return z.NEVER;
}
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment