-
Notifications
You must be signed in to change notification settings - Fork 2
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 OUTERMOST_OBJ
and OUTERMOST_ARR
allowances
#9
base: main
Are you sure you want to change the base?
Conversation
Run & review this pull request in StackBlitz Codeflow. |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Reviewer's Guide by SourceryThis pull request adds new allowances for parsing outermost objects and arrays in partial JSON, while maintaining stricter parsing for nested elements. It introduces OUTERMOST_OBJ and OUTERMOST_ARR flags, updates the parsing logic to handle these new allowances, and improves error handling and partial parsing capabilities. File-Level Changes
Tips
|
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.
Hey @alanpog - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 3 issues found
- 🟢 Security: all looks good
- 🟢 Review instructions: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟡 Documentation: 2 issues found
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
Thanks @alanpog! I think this is useful at least myself. I once had use cases where I needed to disallow incomplete child containers(object/array) while allowing the top-level container to be partial. At that time I resolved array-related problems by simply dropping the last item or something else, and resolved object-related ones by using zod to validate them. Your proposal is great and this implementation works well. But due to my perfectionist tendencies, I often find myself wondering if there's an even better way to solve these kinds of problems. For example:
It seems quite impossible to achieve this through a single So I think this implementation is still not perfect enough. By the way, the diff is too large to review. That's my fault not to include a prettier config and I've added it just now. |
I have some ideas about supporting these complex allow strategies elegantly, but I'm not sure: Approach One — Input Predicate FunctionThe interface options {
allow: number;
validate(parsed: any, text: string, parents: Parent[]): boolean
}
type Parent = {
type: "OBJ";
key: string;
} | {
type: "ARR";
index: number;
} For example, if user do this: parse(`[0, {"a": [{`, { allow: ALL, (parsed, text, parents) => { ... } }) The validate function will be called at most 4 times with validate({}, '{', [{ type: "ARR", index: 0}, { type: "OBJ", key: "a" }, { type: "ARR", index: 1}]) validate([{}], '[{', [{ type: "OBJ", key: "a" }, { type: "ARR", index: 1}]) validate({"a": {}}, '{"a": [{', [{ type: "ARR", index: 1}]) validate([0, {"a": {}}], '[0, {"a": [{', []) Approach Two — Return PartialInfoInject some information into the return value. Like this: export const partial = Symbol('__partial__'); And the partial information may be like this: interface PartialInfo {
text: string;
} Then users can filter the result themselves using this information. For example, if using this way: > res = parse(`[0, {"a": [{`, { allow: ALL, inject: true }) // [0, {"a": [{}]}]
> res[0][partial] // undefined
> res[1][partial] // { text: '{"a": [{' }
> res[1].a[partial] // { text: '[{' }
> res[1].a[0][partial] // { text: '{' } They can drop at any depth level as they wish. |
OUTERMOST_OBJ
and OUTERMOST_ARR
allowances
Co-authored-by: Muspi Merol <[email protected]>
Not sure if relevant for the general library here, but sending this PR in case you find it useful (it was needed for our specific use case). These changes intend to allow partials for the outermost objects and arrays (i.e., objects that don't have another object up in the json hierarchy and similarly for arrays), but disallow it for any non-outermost objects or arrays (avoiding the words root and non-nested as their definitions are usually slightly different).
The
README.md
has some explanation and there are quite a few test cases too.I couldn't find a .prettierrc in the repo and ended up unintentionally messing up with the formatting :(
Summary by Sourcery
Add new allowances OUTERMOST_OBJ and OUTERMOST_ARR to support partial parsing of outermost JSON objects and arrays. Enhance the JSON parsing logic to track depth and improve handling of partial structures. Update documentation to reflect these changes.
New Features:
Enhancements:
Documentation: