-
Notifications
You must be signed in to change notification settings - Fork 709
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
add core validator generator for Object-type props #8878
add core validator generator for Object-type props #8878
Conversation
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.
A good start, a test suite would help to demonstrate that the validator is working as intended. Some cleanup and simplification can be done too.
@@ -25,3 +25,195 @@ export function validateLearningActivity(arr) { | |||
const isValidLearningActivity = v => Object.values(LearningActivities).includes(v); | |||
return arr.length > 0 && arr.every(isValidLearningActivity); | |||
} | |||
export function objectPropValidator( options ){ |
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.
Would be good to have a test suite to demonstrate the validation happening here.
} catch {} | ||
return match ? match[1] : (rawType.length > 0 ? rawType : ((typeof variable == "function" || typeof variable.name != "undefined") ? variable.name : '')); | ||
}; | ||
var _isUndefined = function(variable) { |
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 would suggest using lodash helper functions here e.g. https://lodash.com/docs/4.17.15#isBoolean instead of defining your own functions here.
var _isUndefined = function(variable) { | ||
return (typeof variable === "undefined"); | ||
}; | ||
var _isSet = function(variable) { |
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.
This function name is confusing, as Set
is an ES6 native type.
@@ -25,3 +25,195 @@ export function validateLearningActivity(arr) { | |||
const isValidLearningActivity = v => Object.values(LearningActivities).includes(v); | |||
return arr.length > 0 && arr.every(isValidLearningActivity); | |||
} | |||
export function objectPropValidator( options ){ | |||
var args = arguments || []; |
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.
Preferable to use ES6 const
and let
declarations.
@@ -25,3 +25,195 @@ export function validateLearningActivity(arr) { | |||
const isValidLearningActivity = v => Object.values(LearningActivities).includes(v); | |||
return arr.length > 0 && arr.every(isValidLearningActivity); | |||
} | |||
export function objectPropValidator( options ){ |
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 overall this could be simplified and cleaned up with a type map:
import isNumber from 'lodash/isNumber';
import isBoolean from 'lodash/isBoolean';
const typeValidatorMap = {
Number: isNumber,
Boolean: isBoolean,
}
This is an example, but could be extended to all supported types.
Then when going to check can just do:
typeValidatorMap[option.type](data);
Minor request: could we please rename |
also, please check the linting and formatting: https://kolibri-dev.readthedocs.io/en/develop/getting_started.html#linting-and-auto-formatting |
Hi @hamzamunaf – thank you very much for your work! I wanted to move forward with using the results in some ongoing refactoring I'm doing, so I took your commit and integrated it into a new PR here along with the above feedback from @rtibbles: #8902, so I'll close this as superseded. Your review of the new PR would be appreciated. I've also opened a new follow-up issue here: #8903 – if you're interested in continuing, your help there would be appreciated! |
Summary
Adding Core Validator Generator for Object Type Props
…
References
Closes #8640
…
Reviewer guidance
Use the ObjectPropValidator function like this:
…
Testing checklist
PR process
Reviewer checklist
yarn
andpip
)