File tree 2 files changed +68
-1
lines changed
packages/to-json-schema/src
2 files changed +68
-1
lines changed Original file line number Diff line number Diff line change @@ -490,4 +490,55 @@ describe('convertAction', () => {
490
490
'The "transform" action cannot be converted to JSON Schema.'
491
491
) ;
492
492
} ) ;
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
+ } ) ;
493
544
} ) ;
Original file line number Diff line number Diff line change @@ -56,7 +56,11 @@ type Action =
56
56
number ,
57
57
v . ErrorMessage < v . MultipleOfIssue < number , number > > | undefined
58
58
>
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
+ > ;
60
64
61
65
/**
62
66
* Converts any supported Valibot action to the JSON Schema format.
@@ -191,6 +195,18 @@ export function convertAction(
191
195
break ;
192
196
}
193
197
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
+
194
210
default : {
195
211
throwOrWarn (
196
212
// @ts -expect-error
You can’t perform that action at this time.
0 commit comments