-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support for valueType:Array #195
Comments
To fix assertValueType.js, add this after line 34: else if ( valueType === Array ) {
assert( Array.isArray( value ), 'value should be an Array, value=' + value );
} Does that seem like an acceptable solution? |
The above passes lint. But WebStorm is complaining about |
GRAPHING_QUADRATICS/GQModel appears to be the only place where |
Signed-off-by: Chris Malley <[email protected]>
Since I don't have time to be a trailblazer here, I changed GRAPHING_QUADRATICS/GQModel to handle the Array.toArray call, i.e.: // Options for {Property.<Quadratic[]>}
const optionsPropertyQuadraticArray = {
isValidValue: array => Array.isArray( array ) &&
_.every( array, function( value ) { return value instanceof Quadratic; } )
}; Removing high priority since this no longer affects Graphing Quadratics. Leaving this issue open to decide how to handle in general. |
The |
On hold until #189 is decided. |
I think #189 is far enough along to bring this back up for discussion. I have two potential proposals:
|
Writing down some options. // Proposal 1 from preceding comment
new Property(myArray,valueType:['number'])
new Property(myArray,valueType:[Vector2])
// Proposal 2 from preceding comment
new Property(myArray,isValidValue: Validator.getArrayValidator('number'));
// Option 3: checking that it is an array and all values match a spec, using Validator and existing support
new Property(myArray,{isValidValue: v=>Array.isArray(v) && _.every(v, element=>Validator.validate(element,{valueType: 'number'})}
// Option 4: new key type
new Property(myArray,{arrayElementType: 'number'});
new Property(myArray,{arrayElementType: Vector2}); 1 and 4 seem nice. In #189 (comment) we discussed keeping typeof and instanceof bound together in // Option 5: nested validator for arrays
new Property(myArray, {arrayElementType:{validValues: [1,2,3]}} // array of values 1,2,3 only
new Property(myArray, {arrayElementType:{isValidValue: x => x>0;}} // array of positive numbers |
My assumption in the solution proposed in #195 (comment) was that checking that the value is an |
And what if you have an Array of Arrays of Arrays of... ? Do you propose to drill all the way down to test the types of the leaf elements? |
I've found that helpful in the past in a few cases, usually doing No strong feelings, but it seems if Validator can see the Array type and convert the check to |
I don't want to choose a validation strategy that makes that impossible. Option 5 from above makes this possible via: // Array of arrays of positive values
{arrayElementType:{arrayElementType:{isValidValue: v=> v>0}}}
That is already supported via |
I really think that something like:
... would be sufficient to handle validation of Arrays (and Arrays of Arrays, etc.) If you really want to incur the cost/complexity of building that Array-specific support into Validator, go for it. But I think the payoff will be minimal. |
I think that
|
That's pretty much what I proposed in #195 (comment). |
I think that naming it I think there are two leading ideas about how we go, please correct me if I am mistaken:
new Property( ..., {
valueType: Array,
isValidValue: value => value.forEach((element)=>typeof element === 'string'); // predicate to validate what's in Array
} );
I think that including It's good to note that this has always been a problem for Property validation, the issue has just moved over to being Do people like option 1 or 2 or another thing better? |
Option 1 seems sufficient to me, as I said in #195 (comment). |
@zepumph @chrisklus and I added support for |
I think |
Yes I think this is most efficient and best way forward right now. |
This came up again as part of #227. Reopening. As discussion with @pixelzoom and @samreid, it seems like we should make a new validator key like;
which will basically do:
|
The other common case I’m running into is {
// {SomeType|null}
isValidValue: value => ( value instanceof SomeType || value === null )
} |
see #228 |
Everything looks great, and the unit tests have good coverage, nice work! Thanks. Closing. |
Noted during Graphing Quadratics code review phetsims/graphing-quadratics#43, when working on this checklist item:
"Use Array.isArray to type check an array" does not appear to be the case in Property validation.
Example in GRAPHING_QUADRATICS/GQModel:
Property's
valueType
option is handled by assertValueType.js, which contains no special handling forArray
. SovalueType: Array
gets checked usinginstanceof
, notArray.isArray
.assertValueType.js should either be fixed to handle Array properly, or an assertion should be added to disallow
valueType: Array
. If the latter, then the above code would need to change to:@samreid @jonathanolson opinions? Keep in mind that we're discussing unifying the various validation options in #189.
High priority because this affects Graphing Quadratics.
The text was updated successfully, but these errors were encountered: