Skip to content

Commit

Permalink
Add support for valueType: Array, see #195
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jan 16, 2019
1 parent 0d4f72c commit 4dcf357
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 10 additions & 7 deletions js/Validator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018, University of Colorado Boulder
// Copyright 2018-2019, University of Colorado Boulder

/**
* Throws assertion errors if a value doesn't match the specified criteria.
Expand Down Expand Up @@ -65,10 +65,13 @@ define( require => {
if ( options.valueType ) {
const valueType = options.valueType;
if ( typeof valueType === 'string' ) { // primitive type
assert( typeof value === valueType, 'value should have typeof ' + valueType + ', value=' + value );
assert( typeof value === valueType, `value should have typeof ${valueType}, value=${value}` );
}
else if ( typeof valueType === 'function' ) { // constructor
assert( value instanceof valueType, 'value should be instanceof ' + valueType.name + ', value=' + value );
assert( value instanceof valueType, `value should be instanceof ${valueType.name}, value=${value}` );
}
else if ( valueType === Array ) {
assert( Array.isArray( value ), `value should have been an array, value=${value}` );
}
}
options.validValues && assert( options.validValues.indexOf( value ) >= 0, `value not in validValues: ${value}` );
Expand Down Expand Up @@ -97,25 +100,25 @@ define( require => {
typeof valueType === 'string' ||
valueType === null ||
valueType === undefined,
'valueType must be {function|string|null}, valueType=' + valueType
`valueType must be {function|string|null}, valueType=${valueType}`
);

// {string} valueType must be one of the primitives in TYPEOF_STRINGS, for typeof comparison
if ( typeof valueType === 'string' ) {
assert( _.includes( TYPEOF_STRINGS, valueType ), 'valueType not a supported primitive types: ' + valueType );
assert( _.includes( TYPEOF_STRINGS, valueType ), `valueType not a supported primitive types: ${valueType}` );
}

if ( options.hasOwnProperty( 'isValidValue' ) ) {
assert(
options.isValidValue === null ||
typeof options.isValidValue === 'function' ||
options.isValidValue === undefined,
'isValidValue must be a function: ' + options.isValidValue
`isValidValue must be a function: ${options.isValidValue}`
);
}

if ( options.validValues !== undefined && options.validValues !== null ) {
assert( Array.isArray( options.validValues ), 'validValues must be an array: ' + options.validValues );
assert( Array.isArray( options.validValues ), `validValues must be an array: ${options.validValues}` );

// Make sure each initial value is valid.
const remainingOptions = _.omit( options, 'validValues' );
Expand Down
16 changes: 7 additions & 9 deletions js/ValidatorTests.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
// Copyright 2017-2018, University of Colorado Boulder
// Copyright 2017-2019, University of Colorado Boulder

/**
* QUnit tests for Validator
*
* @author Sam Reid (PhET Interactive Simulations)
*/
define( function( require ) {
define( require => {
'use strict';

// modules
var Validator = require( 'AXON/Validator' );
const Validator = require( 'AXON/Validator' );

QUnit.module( 'Validator' );

// Note that many validation tests are in PropertyTests
QUnit.test( 'Test Validator', function( assert ) {
assert.ok( true, 'so we have at least 1 test in this set' );
QUnit.test( 'Test Validator', assert => {

assert.ok( Validator.validate( 3, { validValues: [ 1, 2, 3 ] } ) );

window.assert && assert.throws( function() {
assert.ok( !Validator.validate( 4, { validValues: [ 1, 2, 3 ] } ) );
}, 'should throw assertion error' );
assert.ok( Validator.validate( [], { valueType: Array } ) );
window.assert && assert.throws( () => !Validator.validate( 4, { validValues: [ 1, 2, 3 ] } ), 'invalid number' );
window.assert && assert.throws( () => !Validator.validate( 'hello', { valueType: Array } ), 'string isn\'t Array' );
} );
} );

0 comments on commit 4dcf357

Please sign in to comment.