-
Notifications
You must be signed in to change notification settings - Fork 0
Add array type support (objects) #4
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
base: master
Are you sure you want to change the base?
Conversation
|
Although part 2 of #2 isn't implemented, the lack of |
|
|
||
| if (_.isArray(fieldSpecs[specName])) { | ||
| return values[valueKey].map((arrayValue, index) => { | ||
| return getMismatchedFields( |
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 part can be modified to support primitive arrays. I imagine the way to specify primitive arrays to be like ['number'] or ['boolean'], following your test example. Then to add primitive support here, you need to:
- Extract the primitive check from Line 76 to Line 111 to a function accepting the primitive type and a value.
- Check here if the
fieldSpecs[specName][0]is a string, indicating a primitive type. If it is, use the extracted function to check. Otherwise, callgetMismatchedFields.
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.
Also, in the extracted function, check if the primitive type is one of the expected types, i.e. the string name is correct. You can throw a TypeError if it isn't. This is to provide runtime safety.
| return values[valueKey].map((arrayValue, index) => { | ||
| return getMismatchedFields( | ||
| arrayValue, | ||
| fieldSpecs[specName][0] as Interface, |
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.
Please add a array out-of-bound guard here. If fieldSpecs[specName][0] is undefined, throw a TypeError here. The message can be a JSON.stringified string of the whole fieldSpecs and the specName to help with debugging.
|
Thank you brod. This diff looks good. Although the syntax is not exactly what I imagined (it deviates from TS syntax), I do like the simplicity and potential type safety. I only have the above two change requests. |
hanlindev
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.
Two inline comments
|
Cheers. Agreed on syntax, |
#2
Initial support for arrays of objects - arrays of other types might already work once added to the primitives.
Error responses are OK but will probably need tweaking for non-object array types.