Skip to content

Commit 5c434e6

Browse files
committed
Add support for uniqueItems in convertAction
1 parent e8c83f5 commit 5c434e6

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

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

+51
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,55 @@ describe('convertAction', () => {
490490
'The "transform" action cannot be converted to JSON Schema.'
491491
);
492492
});
493+
494+
test('should convert unique items action for array', () => {
495+
expect(
496+
convertAction(
497+
{
498+
type: 'array',
499+
},
500+
v.uniqueItems<v.ArrayInput>(),
501+
undefined
502+
)
503+
).toStrictEqual({
504+
type: 'array',
505+
uniqueItems: true,
506+
});
507+
});
508+
509+
test('should throw error for unique items action with invalid type', () => {
510+
expect(() =>
511+
convertAction({}, v.uniqueItems<v.ArrayInput>(), undefined)
512+
).toThrowError(
513+
'The "unique_items" action is not supported on type "undefined".'
514+
);
515+
expect(() =>
516+
convertAction(
517+
{ type: 'string' },
518+
v.uniqueItems<v.ArrayInput>(),
519+
undefined
520+
)
521+
).toThrowError(
522+
'The "unique_items" action is not supported on type "string".'
523+
);
524+
});
525+
526+
test('should force conversion for unique items action with invalid type', () => {
527+
expect(
528+
convertAction({}, v.uniqueItems<v.ArrayInput>(), { force: true })
529+
).toStrictEqual({
530+
uniqueItems: true,
531+
});
532+
expect(console.warn).toHaveBeenLastCalledWith(
533+
'The "unique_items" action is not supported on type "undefined".'
534+
);
535+
expect(
536+
convertAction({ type: 'string' }, v.uniqueItems<v.ArrayInput>(), {
537+
force: true,
538+
})
539+
).toStrictEqual({ type: 'string', uniqueItems: true });
540+
expect(console.warn).toHaveBeenLastCalledWith(
541+
'The "unique_items" action is not supported on type "string".'
542+
);
543+
});
493544
});

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ type Action =
5656
number,
5757
v.ErrorMessage<v.MultipleOfIssue<number, number>> | undefined
5858
>
59-
| v.TitleAction<unknown, string>;
59+
| v.TitleAction<unknown, string>
60+
| v.UniqueItemsAction<
61+
v.ArrayInput,
62+
v.ErrorMessage<v.UniqueItemsIssue<v.ArrayInput>> | undefined
63+
>;
6064

6165
/**
6266
* Converts any supported Valibot action to the JSON Schema format.
@@ -191,6 +195,18 @@ export function convertAction(
191195
break;
192196
}
193197

198+
case 'unique_items': {
199+
if (jsonSchema.type !== 'array') {
200+
throwOrWarn(
201+
`The "${valibotAction.type}" action is not supported on type "${jsonSchema.type}".`,
202+
config
203+
);
204+
}
205+
jsonSchema.uniqueItems = true;
206+
207+
break;
208+
}
209+
194210
default: {
195211
throwOrWarn(
196212
// @ts-expect-error

0 commit comments

Comments
 (0)