Skip to content

Commit

Permalink
Simplify pattern for validating values, see #182
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jan 16, 2019
1 parent 52c9a17 commit 2f48b0e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
14 changes: 8 additions & 6 deletions js/Property.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ define( function( require ) {
validateOptionsOnValidateValue: false
}, options );

// @private
this.validatorOptions = Validator.pickOptions( options );

assert && Validator.validateOptions( this.validatorOptions );
assert && Validator.validateOptions( options );

assert && options.units && assert( units.isValidUnits( options.units ), 'invalid units: ' + options.units );
if ( options.units ) {
Expand All @@ -79,8 +76,13 @@ define( function( require ) {
// useDeepEquality: false => Use === for equality test
this.useDeepEquality = options.useDeepEquality;

// @private {function|false} - closure over options for validation in set()
this.validate = assert && function( value ) {
Validator.validate( value, options );
};

// validate the initial value
assert && Validator.validate( value, this.validatorOptions );
assert && this.validate( value );

// When running as phet-io, if the tandem is specified, the type must be specified.
// This assertion helps in instrumenting code that has the tandem but not type
Expand Down Expand Up @@ -148,7 +150,7 @@ define( function( require ) {
* @public
*/
set: function( value ) {
assert && Validator.validate( value, this.validatorOptions );
this.validate && this.validate( value );
if ( this.isDeferred ) {
this.deferredValue = value;
this.hasDeferredValue = true;
Expand Down
12 changes: 1 addition & 11 deletions js/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ define( require => {
validateOptionsOnValidateValue: true
};

const DEFAULT_OPTIONS_KEYS = _.keys( DEFAULT_OPTIONS );

const Validator = {

/**
Expand Down Expand Up @@ -128,15 +126,7 @@ define( require => {
else {
return true;
}
},

/**
* Get only the options that are used by Validator
* @param {Object} [options]
* @returns {Object}
* @public
*/
pickOptions: options => _.pick( options, DEFAULT_OPTIONS_KEYS )
}
};

Validator.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
Expand Down

0 comments on commit 2f48b0e

Please sign in to comment.