Skip to content

Commit

Permalink
feat: support more actions in JSON schema conversion
Browse files Browse the repository at this point in the history
Adds support for the following actions in JSON schema conversion:

- `bic`
- `cuid2`
- `decimal`
- `digits`
- `empty`
  • Loading branch information
43081j committed Dec 27, 2024
1 parent cf13890 commit a931fd2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
61 changes: 61 additions & 0 deletions packages/to-json-schema/src/convertAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
33 changes: 32 additions & 1 deletion packages/to-json-schema/src/convertAction.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -56,7 +57,15 @@ type Action =
number,
v.ErrorMessage<v.MultipleOfIssue<number, number>> | undefined
>
| v.TitleAction<unknown, string>;
| v.TitleAction<unknown, string>
| v.BicAction<never, v.ErrorMessage<v.BicIssue<string>> | undefined>
| v.Cuid2Action<never, v.ErrorMessage<v.Cuid2Issue<string>> | undefined>
| v.DecimalAction<never, v.ErrorMessage<v.DecimalIssue<string>> | undefined>
| v.DigitsAction<never, v.ErrorMessage<v.DigitsIssue<string>> | undefined>
| v.EmptyAction<
v.LengthInput,
v.ErrorMessage<v.EmptyIssue<v.LengthInput>> | undefined
>;

/**
* Converts any supported Valibot action to the JSON Schema format.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a931fd2

Please sign in to comment.