From 6bba4322bf9dc1a589bf1ff634c606853bde51bf Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 17 Mar 2017 11:17:41 -0400 Subject: [PATCH] ValidStateMonitor: Consider scalar diff to affect all indexes Classifications often yield scalar results. Since those results are used directly in the diff, we have the situation where the expected diff format (an array) is not provided. Consistent with the rest of the system, we should consider a scalar to affect every index. * src/validate/ValidStateMonitor.js (_checkCauseFix): Consider scalar diffs to affect every index when checking for fixes. * test/validate/ValidStateMonitorTest.js: Add test. --- src/validate/ValidStateMonitor.js | 13 ++++++-- test/validate/ValidStateMonitorTest.js | 43 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) 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