@@ -14,22 +14,29 @@ export type ErrorQuery = {
1414 replace : ErrorQueryReplace
1515}
1616
17- export const byError = ( errors : ErrorObject [ ] ) => {
17+ export interface Comparator { ( error : ErrorObject , query : ErrorQuery ) : boolean }
18+
19+ const defaultMatcher : Comparator = ( error , query ) => {
20+ const keys = Object . keys ( query . find )
21+ if ( keys . includes ( 'instancePath' ) && query . find . instancePath !== error . instancePath ) {
22+ return false
23+ }
24+ if ( keys . includes ( 'schemaPath' ) && query . find . schemaPath !== error . schemaPath ) {
25+ return false
26+ }
27+ if ( keys . includes ( 'message' ) && query . find . message !== error . message ) {
28+ return false
29+ }
30+ return true
31+ }
32+
33+ export const duplicateMatcher = ( index : number ) => {
34+ return ( error : ErrorObject ) => error . params . i === index || error . params . j === index
35+ }
36+
37+ export const byError = ( errors : ErrorObject [ ] , matcher : Comparator = defaultMatcher ) => {
1838 return ( query : ErrorQuery ) => {
19- const matches = ( error : ErrorObject ) => {
20- const keys = Object . keys ( query . find )
21- if ( keys . includes ( 'instancePath' ) && query . find . instancePath !== error . instancePath ) {
22- return false
23- }
24- if ( keys . includes ( 'schemaPath' ) && query . find . schemaPath !== error . schemaPath ) {
25- return false
26- }
27- if ( keys . includes ( 'message' ) && query . find . message !== error . message ) {
28- return false
29- }
30- return true
31- }
32- return errors . some ( matches )
39+ return errors . some ( ( error : ErrorObject ) => matcher ( error , query ) )
3340 }
3441}
3542
@@ -62,6 +69,36 @@ export const dateReleasedQueries: ErrorQuery[] = [{
6269 }
6370} ]
6471
72+ export const duplicateAuthorQueries : ErrorQuery [ ] = [ {
73+ find : {
74+ instancePath : '/authors' ,
75+ schemaPath : '#/properties/authors/uniqueItems'
76+ } ,
77+ replace : {
78+ message : 'This author is a duplicate.'
79+ }
80+ } ]
81+
82+ export const duplicateIdentifierQueries : ErrorQuery [ ] = [ {
83+ find : {
84+ instancePath : '/identifiers' ,
85+ schemaPath : '#/properties/identifiers/uniqueItems'
86+ } ,
87+ replace : {
88+ message : 'This identifier is a duplicate.'
89+ }
90+ } ]
91+
92+ export const duplicateKeywordQueries : ErrorQuery [ ] = [ {
93+ find : {
94+ instancePath : '/keywords' ,
95+ schemaPath : '#/properties/keywords/uniqueItems'
96+ } ,
97+ replace : {
98+ message : 'This keyword is a duplicate.'
99+ }
100+ } ]
101+
65102export const emailQueries = ( index : number ) => {
66103 return [ {
67104 find : {
0 commit comments