diff --git a/packages/to-json-schema/src/convertAction.test.ts b/packages/to-json-schema/src/convertAction.test.ts index b48a72020..547bc3c10 100644 --- a/packages/to-json-schema/src/convertAction.test.ts +++ b/packages/to-json-schema/src/convertAction.test.ts @@ -508,4 +508,65 @@ describe('convertAction', () => { 'The "transform" action cannot be converted to JSON Schema.' ); }); + + test('should convert BIC action', () => { + expect(convertAction({ type: 'string' }, v.bic(), undefined)).toStrictEqual( + { + type: 'string', + pattern: v.BIC_REGEX.source, + } + ); + }); + + test('should convert CUID2 action', () => { + expect( + convertAction({ type: 'string' }, v.cuid2(), undefined) + ).toStrictEqual({ + type: 'string', + pattern: v.CUID2_REGEX.source, + }); + }); + + test('should convert decimal action', () => { + expect( + convertAction({ type: 'string' }, v.decimal(), undefined) + ).toStrictEqual({ + type: 'string', + pattern: v.DECIMAL_REGEX.source, + }); + }); + + test('should convert digits action', () => { + expect( + convertAction({ type: 'string' }, v.digits(), undefined) + ).toStrictEqual({ + type: 'string', + pattern: v.DIGITS_REGEX.source, + }); + }); + + test('should convert empty action with strings', () => { + expect( + convertAction({ type: 'string' }, v.empty(), undefined) + ).toStrictEqual({ + type: 'string', + maxLength: 0, + }); + }); + + test('should convert empty action with arrays', () => { + expect( + convertAction({ type: 'array' }, v.empty(), undefined) + ).toStrictEqual({ + type: 'array', + maxItems: 0, + }); + }); + + test('should throw error for unsupported empty action', () => { + const error = 'The "empty" action is not supported on type "number".'; + expect(() => + convertAction({ type: 'number' }, v.empty(), undefined) + ).toThrowError(error); + }); }); diff --git a/packages/to-json-schema/src/convertAction.ts b/packages/to-json-schema/src/convertAction.ts index cc64ef064..b3c681fc4 100644 --- a/packages/to-json-schema/src/convertAction.ts +++ b/packages/to-json-schema/src/convertAction.ts @@ -1,5 +1,6 @@ import type { JSONSchema7 } from 'json-schema'; import type * as v from 'valibot'; +import { BIC_REGEX, CUID2_REGEX, DECIMAL_REGEX, DIGITS_REGEX } from 'valibot'; import type { ConversionConfig } from './type.ts'; import { handleError } from './utils/index.ts'; @@ -56,7 +57,15 @@ type Action = number, v.ErrorMessage> | undefined > - | v.TitleAction; + | v.TitleAction + | v.BicAction> | undefined> + | v.Cuid2Action> | undefined> + | v.DecimalAction> | undefined> + | v.DigitsAction> | undefined> + | v.EmptyAction< + v.LengthInput, + v.ErrorMessage> | undefined + >; /** * Converts any supported Valibot action to the JSON Schema format. @@ -191,6 +200,28 @@ export function convertAction( break; } + case 'bic': + case 'cuid2': + case 'decimal': + case 'digits': { + jsonSchema.pattern = valibotAction.requirement.source; + break; + } + + case 'empty': { + if (jsonSchema.type === 'string') { + jsonSchema.maxLength = 0; + } else if (jsonSchema.type === 'array') { + jsonSchema.maxItems = 0; + } else { + handleError( + `The "empty" action is not supported on type "${jsonSchema.type}".`, + config + ); + } + break; + } + default: { handleError( // @ts-expect-error