Add array type support (objects)#4
Add array type support (objects)#48eecf0d2 wants to merge 3 commits intohanlindev:masterfrom 8eecf0d2:master
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.
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.
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.
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. |
|
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.