Skip to content

Commit 7d32cf3

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 cf13890 commit 7d32cf3

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-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

+39-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,22 @@ 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+
>;
69+
70+
const typeToPatternMap = {
71+
bic: BIC_REGEX,
72+
cuid2: CUID2_REGEX,
73+
decimal: DECIMAL_REGEX,
74+
digits: DIGITS_REGEX,
75+
};
6076

6177
/**
6278
* Converts any supported Valibot action to the JSON Schema format.
@@ -191,6 +207,28 @@ export function convertAction(
191207
break;
192208
}
193209

210+
case 'bic':
211+
case 'cuid2':
212+
case 'decimal':
213+
case 'digits': {
214+
jsonSchema.pattern = typeToPatternMap[valibotAction.type].source;
215+
break;
216+
}
217+
218+
case 'empty': {
219+
if (jsonSchema.type === 'string') {
220+
jsonSchema.maxLength = 0;
221+
} else if (jsonSchema.type === 'array') {
222+
jsonSchema.maxItems = 0;
223+
} else {
224+
handleError(
225+
`The "empty" action is not supported on type "${jsonSchema.type}".`,
226+
config
227+
);
228+
}
229+
break;
230+
}
231+
194232
default: {
195233
handleError(
196234
// @ts-expect-error

0 commit comments

Comments
 (0)