-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Validate schema's default values #2511
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
Conversation
|
Noticed something is happening on default that creates something with And that's giving a warning because of the functions I built. |
src/utils/validate.js
Outdated
| @@ -0,0 +1,61 @@ | |||
| /** | |||
| * Checks if Valid default coordinates | |||
| * @param {unknown} possibleCoordinates | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we give to this file a more precise name? validate-schema.js, validate-type.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Thanks.
src/core/schema.js
Outdated
| propDefinition.type = typeName; | ||
|
|
||
| // Fill in default value. | ||
| if (!('default' in propDefinition)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could swap the if around now so it's not negative:
if ('default' in propDefinition) {
} else {
}
src/utils/validate.js
Outdated
| // * @returns {boolean} A boolean determining if defaults are valid. | ||
| // */ | ||
| function isValidDefaultValue (type, defaultVal) { | ||
| if (type === 'audio' && typeof defaultVal !== 'string') return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add braces around the if statements?
src/utils/validate-schema.js
Outdated
| @@ -0,0 +1,61 @@ | |||
| /** | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd actually just put all of this code within the schema module.
src/utils/validate-schema.js
Outdated
| * @returns {boolean} A boolean determining if coordinates are parsed correctly. | ||
| */ | ||
| function isValidDefaultCoordinate (possibleCoordinates, dimensions) { | ||
| if (typeof possibleCoordinates !== 'object' || possibleCoordinates === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure whether null should be invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm...that does make sense. Since currently if you do not define a default value, when you register a component it will register a default value at { x:0, y:0, z:0 } If you don't want that to happen assigning it to null makes sense. I'll adjust it.
src/utils/validate-schema.js
Outdated
| } else if (Object.keys(possibleCoordinates).length !== dimensions) { | ||
| return false; | ||
| } else { | ||
| if (dimensions === 2 && (possibleCoordinates.x === 0 || possibleCoordinates.x) && (possibleCoordinates.y === 0 || possibleCoordinates.y)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could condense this code more. We only need to check X/Y/Z/W each once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally, I did it a dumb way. 😂
src/utils/validate-schema.js
Outdated
| * @returns {boolean} A boolean determining if defaults are valid. | ||
| */ | ||
| function isValidDefaultValue (type, defaultVal) { | ||
| if (type === 'audio' && typeof defaultVal !== 'string') return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braces around the if code { }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha.
src/utils/validate-schema.js
Outdated
| * @param {unknown} defaultVal - default in a prop | ||
| * @returns {boolean} A boolean determining if defaults are valid. | ||
| */ | ||
| function isValidDefaultValue (type, defaultVal) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we unit test these? (part of the schema.test.js)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Was going to unit test it afterwards since it's quite a bit of tests to write. But I'll get on it.
… into schema, reversed conditional statement for validation warning
…ltCoordinate tests to have a test for null
|
I've made all the changes you suggested @ngokevin. What do you think? Should I write some connection tests? |
dye784
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made all the changes suggested
Description:
What originally was intended to fix issue #2412 with PR #2500 grew into this PR. The intent of this is to enforce properly parsed default values based on a defined type instead of parsing the
defaultinto something acceptable.Changes proposed:
Created
validate.jswhich contains two functions:isValidDefaultCoordinateandisValidDefaultValue.isValidDefaultValuechecks that the type and thetypeofdefault value match. It callsisValidDefaultCoordinateto checkvec2,vec3andvec4.processPropertyDefinitionthrows a warning ifisValidDefaultValuereturnsfalse.