@@ -5,24 +5,24 @@ const map = require('lodash/map');
55const transform = require ( 'lodash/transform' ) ;
66const ValidationError = require ( './validation-error' ) ;
77
8- class ValidationUniquenessError extends ValidationError {
9- constructor ( instance , index ) {
10- super ( ...arguments ) ;
11- this . name = 'ValidationUniquenessError' ;
12- this . index = index ;
13- this . values = parseValues ( instance , index ) ;
14- }
15-
16- get message ( ) {
17- return `${ this . name } : duplicate value for ${ this . index . join ( ) } : ${ map ( this . values ) . join ( ) } ; ${ this . instanceInfo } ` ;
8+ class UniquenessValidationError extends ValidationError {
9+ constructor ( instance , index = [ ] ) {
10+ const values = parseValues ( instance , index ) ;
11+ const message = getMessage ( index , values ) ;
12+ super ( instance , message ) ;
13+ Object . assign ( this , { values, index } ) ;
1814 }
1915}
2016
21- module . exports = ValidationUniquenessError ;
17+ module . exports = UniquenessValidationError ;
2218
2319function parseValues ( instance , index ) {
2420 return transform ( index , ( acc , it ) => {
2521 const attribute = find ( instance . rawAttributes , { field : it } ) . fieldName ;
2622 acc [ it ] = instance [ attribute ] ;
2723 } , { } ) ;
2824}
25+
26+ function getMessage ( index , values ) {
27+ return `Duplicate value for ${ index . join ( ) } : ${ map ( values ) . join ( ) } ` ;
28+ }
0 commit comments