File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -181,6 +181,7 @@ const useAJVForm = <T extends Record<string, any>>(
181
181
setState ( ( prevState ) =>
182
182
Object . keys ( newErrors ) . reduce ( ( updatedState , fieldName ) => {
183
183
return {
184
+ ...prevState ,
184
185
...updatedState ,
185
186
[ fieldName ] : {
186
187
...prevState [ fieldName ] ,
Original file line number Diff line number Diff line change @@ -586,6 +586,44 @@ describe('useAJVForm should properly set errors programmatically using setErrors
586
586
expect ( result . current . state . description . error ) . toBe ( 'Monkey message' ) ;
587
587
} ) ;
588
588
589
+ it ( 'sets the errors using form.setErrors without affecting other state' , ( ) => {
590
+ const initialData = { title : 'Foo' , description : 'Bar' } ;
591
+ const schema : JSONSchemaType < { title : string ; description : string } > = {
592
+ type : 'object' ,
593
+ required : [ 'title' , 'description' ] ,
594
+ properties : {
595
+ title : { type : 'string' } ,
596
+ description : { type : 'string' } ,
597
+ } ,
598
+ } ;
599
+
600
+ const { result } = renderHook ( ( ) =>
601
+ useAJVForm (
602
+ initialData ,
603
+ schema ,
604
+ {
605
+ userDefinedMessages : {
606
+ required : ( ) => 'Monkey message' ,
607
+ } ,
608
+ } ,
609
+ ) ,
610
+ ) ;
611
+
612
+ result . current . setErrors ( [
613
+ {
614
+ instancePath : '/description' ,
615
+ keyword : 'required' ,
616
+ params : { missingProperty : 'description' } ,
617
+ schemaPath : '#/required' ,
618
+ } ,
619
+ ] ) ;
620
+
621
+ expect ( result . current . state . title . value ) . toBe ( 'Foo' ) ;
622
+ expect ( result . current . state . description . value ) . toBe ( 'Bar' ) ;
623
+
624
+ expect ( result . current . state . description . error ) . toBe ( 'Monkey message' ) ;
625
+ } ) ;
626
+
589
627
it ( 'should handle conditional validations on locationType correctly' , ( ) => {
590
628
/**
591
629
* Test Scenario:
You can’t perform that action at this time.
0 commit comments