Skip to content

Commit c28feda

Browse files
Merge pull request #962 from MathurAditya724/main
feat(json): added support for more schema and actions
2 parents 125a0bc + ccb5480 commit c28feda

File tree

3 files changed

+280
-113
lines changed

3 files changed

+280
-113
lines changed

packages/to-json-schema/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to the library will be documented in this file.
55
## v1.0.0 (Month DD, YYYY)
66

77
- Add support for `undefinedable` schema
8+
- Add support for `base64`, `isoTime`, `isoDateTime`, `nonEmpty` and `url` action (pull request #962)
89
- Change Valibot peer dependency to v1.0.0
910
- Change extraction of default value from `nullable`, `nullish` and `optional` schema
1011
- Change `force` to `errorMode` in config for better control (issue #889)

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

+183-79
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import { convertAction } from './convertAction.ts';
55
console.warn = vi.fn();
66

77
describe('convertAction', () => {
8+
test('should convert base64 action', () => {
9+
expect(convertAction({}, v.base64<string>(), undefined)).toStrictEqual({
10+
contentEncoding: 'base64',
11+
});
12+
expect(
13+
convertAction({ type: 'string' }, v.base64<string>(), undefined)
14+
).toStrictEqual({
15+
type: 'string',
16+
contentEncoding: 'base64',
17+
});
18+
});
19+
820
test('should convert description action', () => {
921
expect(convertAction({}, v.description('test'), undefined)).toStrictEqual({
1022
description: 'test',
@@ -34,6 +46,30 @@ describe('convertAction', () => {
3446
});
3547
});
3648

49+
test('should convert IPv4 action', () => {
50+
expect(convertAction({}, v.ipv4<string>(), undefined)).toStrictEqual({
51+
format: 'ipv4',
52+
});
53+
expect(
54+
convertAction({ type: 'string' }, v.ipv4<string>(), undefined)
55+
).toStrictEqual({
56+
type: 'string',
57+
format: 'ipv4',
58+
});
59+
});
60+
61+
test('should convert IPv6 action', () => {
62+
expect(convertAction({}, v.ipv6<string>(), undefined)).toStrictEqual({
63+
format: 'ipv6',
64+
});
65+
expect(
66+
convertAction({ type: 'string' }, v.ipv6<string>(), undefined)
67+
).toStrictEqual({
68+
type: 'string',
69+
format: 'ipv6',
70+
});
71+
});
72+
3773
test('should convert ISO date action', () => {
3874
expect(convertAction({}, v.isoDate<string>(), undefined)).toStrictEqual({
3975
format: 'date',
@@ -46,41 +82,43 @@ describe('convertAction', () => {
4682
});
4783
});
4884

49-
test('should convert ISO timestamp action', () => {
50-
expect(
51-
convertAction({}, v.isoTimestamp<string>(), undefined)
52-
).toStrictEqual({
53-
format: 'date-time',
54-
});
85+
test('should convert ISO date time action', () => {
86+
expect(convertAction({}, v.isoDateTime<string>(), undefined)).toStrictEqual(
87+
{
88+
format: 'date-time',
89+
}
90+
);
5591
expect(
56-
convertAction({ type: 'string' }, v.isoTimestamp<string>(), undefined)
92+
convertAction({ type: 'string' }, v.isoDateTime<string>(), undefined)
5793
).toStrictEqual({
5894
type: 'string',
5995
format: 'date-time',
6096
});
6197
});
6298

63-
test('should convert IPv4 action', () => {
64-
expect(convertAction({}, v.ipv4<string>(), undefined)).toStrictEqual({
65-
format: 'ipv4',
99+
test('should convert ISO time action', () => {
100+
expect(convertAction({}, v.isoTime<string>(), undefined)).toStrictEqual({
101+
format: 'time',
66102
});
67103
expect(
68-
convertAction({ type: 'string' }, v.ipv4<string>(), undefined)
104+
convertAction({ type: 'string' }, v.isoTime<string>(), undefined)
69105
).toStrictEqual({
70106
type: 'string',
71-
format: 'ipv4',
107+
format: 'time',
72108
});
73109
});
74110

75-
test('should convert IPv6 action', () => {
76-
expect(convertAction({}, v.ipv6<string>(), undefined)).toStrictEqual({
77-
format: 'ipv6',
111+
test('should convert ISO timestamp action', () => {
112+
expect(
113+
convertAction({}, v.isoTimestamp<string>(), undefined)
114+
).toStrictEqual({
115+
format: 'date-time',
78116
});
79117
expect(
80-
convertAction({ type: 'string' }, v.ipv6<string>(), undefined)
118+
convertAction({ type: 'string' }, v.isoTimestamp<string>(), undefined)
81119
).toStrictEqual({
82120
type: 'string',
83-
format: 'ipv6',
121+
format: 'date-time',
84122
});
85123
});
86124

@@ -144,68 +182,6 @@ describe('convertAction', () => {
144182
);
145183
});
146184

147-
test('should convert min length action for strings', () => {
148-
expect(
149-
convertAction(
150-
{ type: 'string' },
151-
v.minLength<v.LengthInput, 3>(3),
152-
undefined
153-
)
154-
).toStrictEqual({
155-
type: 'string',
156-
minLength: 3,
157-
});
158-
});
159-
160-
test('should convert min length action for arrays', () => {
161-
expect(
162-
convertAction(
163-
{ type: 'array' },
164-
v.minLength<v.LengthInput, 3>(3),
165-
undefined
166-
)
167-
).toStrictEqual({
168-
type: 'array',
169-
minItems: 3,
170-
});
171-
});
172-
173-
test('should throw error for min length action with invalid type', () => {
174-
const action = v.minLength<v.LengthInput, 3>(3);
175-
const error1 =
176-
'The "min_length" action is not supported on type "undefined".';
177-
expect(() => convertAction({}, action, undefined)).toThrowError(error1);
178-
expect(() =>
179-
convertAction({}, action, { errorMode: 'throw' })
180-
).toThrowError(error1);
181-
const error2 = 'The "min_length" action is not supported on type "object".';
182-
expect(() =>
183-
convertAction({ type: 'object' }, action, undefined)
184-
).toThrowError(error2);
185-
expect(() =>
186-
convertAction({ type: 'object' }, action, { errorMode: 'throw' })
187-
).toThrowError(error2);
188-
});
189-
190-
test('should warn error for min length action with invalid type', () => {
191-
expect(
192-
convertAction({}, v.minLength<v.LengthInput, 3>(3), { errorMode: 'warn' })
193-
).toStrictEqual({
194-
minLength: 3,
195-
});
196-
expect(console.warn).toHaveBeenLastCalledWith(
197-
'The "min_length" action is not supported on type "undefined".'
198-
);
199-
expect(
200-
convertAction({ type: 'object' }, v.minLength<v.LengthInput, 3>(3), {
201-
errorMode: 'warn',
202-
})
203-
).toStrictEqual({ type: 'object', minLength: 3 });
204-
expect(console.warn).toHaveBeenLastCalledWith(
205-
'The "min_length" action is not supported on type "object".'
206-
);
207-
});
208-
209185
test('should convert max length action for strings', () => {
210186
expect(
211187
convertAction(
@@ -317,6 +293,68 @@ describe('convertAction', () => {
317293
);
318294
});
319295

296+
test('should convert min length action for strings', () => {
297+
expect(
298+
convertAction(
299+
{ type: 'string' },
300+
v.minLength<v.LengthInput, 3>(3),
301+
undefined
302+
)
303+
).toStrictEqual({
304+
type: 'string',
305+
minLength: 3,
306+
});
307+
});
308+
309+
test('should convert min length action for arrays', () => {
310+
expect(
311+
convertAction(
312+
{ type: 'array' },
313+
v.minLength<v.LengthInput, 3>(3),
314+
undefined
315+
)
316+
).toStrictEqual({
317+
type: 'array',
318+
minItems: 3,
319+
});
320+
});
321+
322+
test('should throw error for min length action with invalid type', () => {
323+
const action = v.minLength<v.LengthInput, 3>(3);
324+
const error1 =
325+
'The "min_length" action is not supported on type "undefined".';
326+
expect(() => convertAction({}, action, undefined)).toThrowError(error1);
327+
expect(() =>
328+
convertAction({}, action, { errorMode: 'throw' })
329+
).toThrowError(error1);
330+
const error2 = 'The "min_length" action is not supported on type "object".';
331+
expect(() =>
332+
convertAction({ type: 'object' }, action, undefined)
333+
).toThrowError(error2);
334+
expect(() =>
335+
convertAction({ type: 'object' }, action, { errorMode: 'throw' })
336+
).toThrowError(error2);
337+
});
338+
339+
test('should warn error for min length action with invalid type', () => {
340+
expect(
341+
convertAction({}, v.minLength<v.LengthInput, 3>(3), { errorMode: 'warn' })
342+
).toStrictEqual({
343+
minLength: 3,
344+
});
345+
expect(console.warn).toHaveBeenLastCalledWith(
346+
'The "min_length" action is not supported on type "undefined".'
347+
);
348+
expect(
349+
convertAction({ type: 'object' }, v.minLength<v.LengthInput, 3>(3), {
350+
errorMode: 'warn',
351+
})
352+
).toStrictEqual({ type: 'object', minLength: 3 });
353+
expect(console.warn).toHaveBeenLastCalledWith(
354+
'The "min_length" action is not supported on type "object".'
355+
);
356+
});
357+
320358
test('should convert min value action for numbers', () => {
321359
expect(
322360
convertAction(
@@ -380,6 +418,60 @@ describe('convertAction', () => {
380418
});
381419
});
382420

421+
test('should convert non empty action for strings', () => {
422+
expect(
423+
convertAction({ type: 'string' }, v.nonEmpty(), undefined)
424+
).toStrictEqual({
425+
type: 'string',
426+
minLength: 1,
427+
});
428+
});
429+
430+
test('should convert non empty action for arrays', () => {
431+
expect(
432+
convertAction({ type: 'array' }, v.nonEmpty(), undefined)
433+
).toStrictEqual({
434+
type: 'array',
435+
minItems: 1,
436+
});
437+
});
438+
439+
test('should throw error for non empty action with invalid type', () => {
440+
const action = v.nonEmpty();
441+
const error1 =
442+
'The "non_empty" action is not supported on type "undefined".';
443+
expect(() => convertAction({}, action, undefined)).toThrowError(error1);
444+
expect(() =>
445+
convertAction({}, action, { errorMode: 'throw' })
446+
).toThrowError(error1);
447+
const error2 = 'The "non_empty" action is not supported on type "object".';
448+
expect(() =>
449+
convertAction({ type: 'object' }, action, undefined)
450+
).toThrowError(error2);
451+
expect(() =>
452+
convertAction({ type: 'object' }, action, { errorMode: 'throw' })
453+
).toThrowError(error2);
454+
});
455+
456+
test('should warn error for non empty action with invalid type', () => {
457+
expect(
458+
convertAction({}, v.nonEmpty(), { errorMode: 'warn' })
459+
).toStrictEqual({
460+
minLength: 1,
461+
});
462+
expect(console.warn).toHaveBeenLastCalledWith(
463+
'The "non_empty" action is not supported on type "undefined".'
464+
);
465+
expect(
466+
convertAction({ type: 'object' }, v.nonEmpty(), {
467+
errorMode: 'warn',
468+
})
469+
).toStrictEqual({ type: 'object', minLength: 1 });
470+
expect(console.warn).toHaveBeenLastCalledWith(
471+
'The "non_empty" action is not supported on type "object".'
472+
);
473+
});
474+
383475
test('should convert supported regex action', () => {
384476
expect(
385477
convertAction({ type: 'string' }, v.regex<string>(/[a-zA-Z]/), undefined)
@@ -418,6 +510,18 @@ describe('convertAction', () => {
418510
});
419511
});
420512

513+
test('should convert url action', () => {
514+
expect(convertAction({}, v.url<string>(), undefined)).toStrictEqual({
515+
format: 'uri',
516+
});
517+
expect(
518+
convertAction({ type: 'string' }, v.url<string>(), undefined)
519+
).toStrictEqual({
520+
type: 'string',
521+
format: 'uri',
522+
});
523+
});
524+
421525
test('should convert UUID action', () => {
422526
expect(convertAction({}, v.uuid<string>(), undefined)).toStrictEqual({
423527
format: 'uuid',

0 commit comments

Comments
 (0)