1
0
Fork 0

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.
master
Mike Gerwitz 2017-03-17 11:17:41 -04:00
parent 2138ddf81a
commit 6bba4322bf
2 changed files with 54 additions and 2 deletions

View File

@ -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;
}

View File

@ -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