diff --git a/src/validate/ValidStateMonitor.js b/src/validate/ValidStateMonitor.js index a25a79a..7b9d0db 100644 --- a/src/validate/ValidStateMonitor.js +++ b/src/validate/ValidStateMonitor.js @@ -277,6 +277,9 @@ module.exports = Class( 'ValidStateMonitor' ) * fix. If so, the promise is fulfilled with the fix data. It is the * responsibility of the caller to handle removing past failures. * + * If the diff contains a scalar instead of an array diff, it is + * considered to affect every index. + * * @param {Object} data validated data * @param {Object} fail failure records * @param {Promise} causep cause promise to chain onto @@ -293,15 +296,21 @@ module.exports = Class( 'ValidStateMonitor' ) new Promise( ( keepgoing, found ) => data.get( cause_name ).then( field => { + // we want everything to be an array, but we need a sane + // fallback if we _are_ provided with scalars + const index_data = ( Array.isArray( field ) ) + ? field[ cause_index ] + : field; + // to be marked as fixed, there must both me no failure // and there must be data for this index for the field // in question (if the field wasn't touched, then of // course there's no failure!) if ( ( ( fail === undefined ) || !( fail[ cause_index ] ) ) - && ( field[ cause_index ] !== undefined ) + && ( index_data !== undefined ) ) { - found( field[ cause_index ] ); + found( index_data ); return; } diff --git a/test/validate/ValidStateMonitorTest.js b/test/validate/ValidStateMonitorTest.js index ca140df..082bcd9 100644 --- a/test/validate/ValidStateMonitorTest.js +++ b/test/validate/ValidStateMonitorTest.js @@ -424,6 +424,49 @@ describe( 'ValidStateMonitor', function() } ); + // if the field diff is a scalar (normally it should be an + // array), it must recognize it as a fix for all indexes + it( 'recognizes scalar diff as fix to any index', function() + { + // scalar + const update_data = { cause: 1 }; + + return new Promise( ( accept, reject ) => + { + return mkstore( update_data ).then( data => + { + return Sut() + .on( 'fix', function( fixed ) + { + expect( fixed ) + .to.deep.equal( { foo: [ 1, 1 ] } ); + + accept(); + } ) + .update( data, { + foo: [ + Failure( + Field( 'foo', 0 ), + '', + [ Field( 'cause', 0 ) ] + ), + Failure( + Field( 'foo', 1 ), + '', + [ Field( 'cause', 1 ) ] + ), + ], + } ) + .then( sut => + { + return sut.update( data, {} ); + } ); + } ) + .catch( e => reject( e ) ); + } ); + } ); + + it( 'considers any number of causes', function() { // different index