Skip to content

Commit 274c950

Browse files
committed
feat: support more actions in JSON schema conversion
Adds support for the following actions in JSON schema conversion: - `bic` - `cuid2` - `decimal` - `digits` - `empty`
1 parent 882f5f1 commit 274c950

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

packages/to-json-schema/src/convertAction.test.ts

+61
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,65 @@ describe('convertAction', () => {
508508
'The "transform" action cannot be converted to JSON Schema.'
509509
);
510510
});
511+
512+
test('should convert BIC action', () => {
513+
expect(convertAction({ type: 'string' }, v.bic(), undefined)).toStrictEqual(
514+
{
515+
type: 'string',
516+
pattern: v.BIC_REGEX.source,
517+
}
518+
);
519+
});
520+
521+
test('should convert CUID2 action', () => {
522+
expect(
523+
convertAction({ type: 'string' }, v.cuid2(), undefined)
524+
).toStrictEqual({
525+
type: 'string',
526+
pattern: v.CUID2_REGEX.source,
527+
});
528+
});
529+
530+
test('should convert decimal action', () => {
531+
expect(
532+
convertAction({ type: 'string' }, v.decimal(), undefined)
533+
).toStrictEqual({
534+
type: 'string',
535+
pattern: v.DECIMAL_REGEX.source,
536+
});
537+
});
538+
539+
test('should convert digits action', () => {
540+
expect(
541+
convertAction({ type: 'string' }, v.digits(), undefined)
542+
).toStrictEqual({
543+
type: 'string',
544+
pattern: v.DIGITS_REGEX.source,
545+
});
546+
});
547+
548+
test('should convert empty action with strings', () => {
549+
expect(
550+
convertAction({ type: 'string' }, v.empty(), undefined)
551+
).toStrictEqual({
552+
type: 'string',
553+
maxLength: 0,
554+
});
555+
});
556+
557+
test('should convert empty action with arrays', () => {
558+
expect(
559+
convertAction({ type: 'array' }, v.empty(), undefined)
560+
).toStrictEqual({
561+
type: 'array',
562+
maxItems: 0,
563+
});
564+
});
565+
566+
test('should throw error for unsupported empty action', () => {
567+
const error = 'The "empty" action is not supported on type "number".';
568+
expect(() =>
569+
convertAction({ type: 'number' }, v.empty(), undefined)
570+
).toThrowError(error);
571+
});
511572
});

packages/to-json-schema/src/convertAction.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { JSONSchema7 } from 'json-schema';
22
import type * as v from 'valibot';
3+
import { BIC_REGEX, CUID2_REGEX, DECIMAL_REGEX, DIGITS_REGEX } from 'valibot';
34
import type { ConversionConfig } from './type.ts';
45
import { handleError } from './utils/index.ts';
56

@@ -56,7 +57,15 @@ type Action =
5657
number,
5758
v.ErrorMessage<v.MultipleOfIssue<number, number>> | undefined
5859
>
59-
| v.TitleAction<unknown, string>;
60+
| v.TitleAction<unknown, string>
61+
| v.BicAction<never, v.ErrorMessage<v.BicIssue<string>> | undefined>
62+
| v.Cuid2Action<never, v.ErrorMessage<v.Cuid2Issue<string>> | undefined>
63+
| v.DecimalAction<never, v.ErrorMessage<v.DecimalIssue<string>> | undefined>
64+
| v.DigitsAction<never, v.ErrorMessage<v.DigitsIssue<string>> | undefined>
65+
| v.EmptyAction<
66+
v.LengthInput,
67+
v.ErrorMessage<v.EmptyIssue<v.LengthInput>> | undefined
68+
>;
6069

6170
/**
6271
* Converts any supported Valibot action to the JSON Schema format.
@@ -191,6 +200,28 @@ export function convertAction(
191200
break;
192201
}
193202

203+
case 'bic':
204+
case 'cuid2':
205+
case 'decimal':
206+
case 'digits': {
207+
jsonSchema.pattern = valibotAction.requirement.source;
208+
break;
209+
}
210+
211+
case 'empty': {
212+
if (jsonSchema.type === 'string') {
213+
jsonSchema.maxLength = 0;
214+
} else if (jsonSchema.type === 'array') {
215+
jsonSchema.maxItems = 0;
216+
} else {
217+
handleError(
218+
`The "empty" action is not supported on type "${jsonSchema.type}".`,
219+
config
220+
);
221+
}
222+
break;
223+
}
224+
194225
default: {
195226
handleError(
196227
// @ts-expect-error

0 commit comments

Comments
 (0)