@@ -3,13 +3,14 @@ import Ajv from 'ajv';
33import get from 'lodash/get' ;
44import isEqual from 'lodash/isEqual' ;
55import isString from 'lodash/isString' ;
6+ import mapValues from 'lodash/mapValues' ;
67import omit from 'lodash/omit' ;
78import set from 'lodash/set' ;
89
9- import { ARRAY_AND_OBJECT_ERRORS , EMPTY_OBJECT , JsonSchemaType } from '../constants' ;
10- import type { ValidationState , ValidationWaiter } from '../mutators' ;
10+ import { ARRAY_AND_OBJECT_ERRORS , EMPTY_OBJECT , JsonSchemaType , OBJECT_ERROR } from '../constants' ;
11+ import type { ErrorsState , ValidationState , ValidationWaiter } from '../mutators' ;
1112import type { FieldValue , JsonSchema , ObjectValue , SyncValidateError , Validator } from '../types' ;
12- import { parseFinalFormPath } from '../utils' ;
13+ import { getSchemaByFinalFormPath , parseFinalFormPath } from '../utils' ;
1314
1415import type {
1516 EntityParametersError ,
@@ -73,6 +74,10 @@ const parseSchemaPath = (schemaPath: string): string[] => {
7374} ;
7475
7576const parseInstancePath = ( instancePath : string ) : string [ ] => {
77+ if ( ! instancePath . length ) {
78+ return [ ] ;
79+ }
80+
7681 return instancePath
7782 . slice ( '/' . length )
7883 . split ( '/' )
@@ -85,7 +90,11 @@ const getSchemaBySchemaPath = (
8590) : JsonSchema | undefined => {
8691 const pathArr = parseSchemaPath ( schemaPath ) ;
8792
88- return pathArr . length ? get ( mainSchema , pathArr ) : mainSchema ;
93+ if ( ! pathArr . length ) {
94+ return mainSchema ;
95+ }
96+
97+ return get ( mainSchema , pathArr ) ;
8998} ;
9099
91100const getSchemaByInstancePath = (
@@ -148,7 +157,7 @@ const getAjvErrorMessage = ({
148157export const getValidate = < Schema extends JsonSchema > ( {
149158 config,
150159 errorMessages,
151- name ,
160+ headName ,
152161 mainSchema,
153162 serviceFieldName,
154163 setValidationCache,
@@ -157,15 +166,17 @@ export const getValidate = <Schema extends JsonSchema>({
157166 const ajvValidate = getAjvValidate ( { config, mainSchema} ) ;
158167
159168 return ( _value , allValues , meta ) => {
160- ajvValidate ( get ( allValues , name ) ) ;
169+ ajvValidate ( get ( allValues , headName ) ) ;
161170
162171 if ( ! ajvValidate . errors ?. length ) {
163172 return false ;
164173 }
165174
175+ const result = { [ ARRAY_AND_OBJECT_ERRORS ] : { } } ;
176+
166177 const waiters : Record < string , ValidationWaiter > = { } ;
167- const ajvErrors : { instancePath : string ; error : SyncValidateError } [ ] = [ ] ;
168- const entityParametersErrors : { instancePath : string ; error : SyncValidateError } [ ] = [ ] ;
178+ const ajvErrors : { path : string [ ] ; error : SyncValidateError } [ ] = [ ] ;
179+ const entityParametersErrors : { path : string [ ] ; error : SyncValidateError } [ ] = [ ] ;
169180
170181 ajvValidate . errors . forEach ( ( ajvOrEntityParametersError ) => {
171182 if ( ajvOrEntityParametersError . keyword === 'entityParameters' ) {
@@ -180,7 +191,10 @@ export const getValidate = <Schema extends JsonSchema>({
180191
181192 if ( cacheItem ?. result ) {
182193 entityParametersErrors . push ( {
183- instancePath : entityParametersError . instancePath ,
194+ path : [
195+ ...parseFinalFormPath ( headName ) ,
196+ ...parseInstancePath ( entityParametersError . instancePath ) ,
197+ ] ,
184198 error : cacheItem . result ,
185199 } ) ;
186200 } else if ( ! waiter || ! isEqual ( entityParametersError . params , waiter ) ) {
@@ -205,7 +219,10 @@ export const getValidate = <Schema extends JsonSchema>({
205219 } ) ;
206220 } else {
207221 entityParametersErrors . push ( {
208- instancePath : entityParametersError . instancePath ,
222+ path : [
223+ ...parseFinalFormPath ( headName ) ,
224+ ...parseInstancePath ( entityParametersError . instancePath ) ,
225+ ] ,
209226 error : errorOrPromise ,
210227 } ) ;
211228 }
@@ -225,7 +242,10 @@ export const getValidate = <Schema extends JsonSchema>({
225242 }
226243
227244 ajvErrors . push ( {
228- instancePath,
245+ path : [
246+ ...parseFinalFormPath ( headName ) ,
247+ ...parseInstancePath ( ajvError . instancePath ) ,
248+ ] ,
229249 error : getAjvErrorMessage ( {
230250 ajvErrorMessage : ajvError . message ,
231251 errorMessages,
@@ -242,25 +262,36 @@ export const getValidate = <Schema extends JsonSchema>({
242262 setValidationWaiters ( { name : serviceFieldName , waiters} ) ;
243263 }
244264
245- const result = { [ ARRAY_AND_OBJECT_ERRORS ] : { } } ;
265+ const errorsState : ErrorsState | undefined = meta ?. data ;
266+ const externalRegularErrors : { path : string [ ] ; error : SyncValidateError } [ ] = Object . values (
267+ mapValues ( errorsState ?. regularErrors , ( value , key ) => ( {
268+ path : parseFinalFormPath ( key ) ,
269+ error : value ,
270+ } ) ) ,
271+ ) ;
272+ const externalPriorityErrors : { path : string [ ] ; error : SyncValidateError } [ ] = Object . values (
273+ mapValues ( errorsState ?. priorityErrors , ( value , key ) => ( {
274+ path : parseFinalFormPath ( key ) ,
275+ error : value ,
276+ } ) ) ,
277+ ) ;
246278
247- [ ...ajvErrors , ...entityParametersErrors ] . forEach ( ( item ) => {
279+ [
280+ ...externalRegularErrors ,
281+ ...ajvErrors ,
282+ ...entityParametersErrors ,
283+ ...externalPriorityErrors ,
284+ ] . forEach ( ( item ) => {
248285 if ( item . error ) {
249- const itemSchema = getSchemaByInstancePath ( item . instancePath , mainSchema ) ;
286+ const itemSchema = getSchemaByFinalFormPath ( item . path , headName , mainSchema ) ;
250287
251288 if ( itemSchema ) {
252- const arrayOrObjectSchema =
253- itemSchema . type === JsonSchemaType . Array ||
254- itemSchema . type === JsonSchemaType . Object ;
255-
256- const path = [
257- ...parseFinalFormPath ( name ) ,
258- ...parseInstancePath ( item . instancePath ) ,
259- ] ;
289+ const arraySchema = itemSchema . type === JsonSchemaType . Array ;
290+ const objectSchema = itemSchema . type === JsonSchemaType . Object ;
260291
261292 set (
262- arrayOrObjectSchema ? result [ ARRAY_AND_OBJECT_ERRORS ] : result ,
263- path ,
293+ arraySchema || objectSchema ? result [ ARRAY_AND_OBJECT_ERRORS ] : result ,
294+ objectSchema ? [ ... item . path , OBJECT_ERROR ] : item . path ,
264295 item . error ,
265296 ) ;
266297 }
0 commit comments