Open alanpog opened 2 months ago
Run & review this pull request in StackBlitz Codeflow.
The latest updates on your projects. Learn more about Vercel for Git āļø
Name | Status | Preview | Comments | Updated (UTC) |
---|---|---|---|---|
partial-json-coverage | ā Ready (Inspect) | Visit Preview | š¬ Add feedback | Sep 17, 2024 1:00pm |
This 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.
Change | Details | Files |
---|---|---|
Added new allowances for outermost objects and arrays |
|
src/options.ts README.md |
Updated parsing logic to handle new allowances |
|
src/index.ts |
Improved error handling and partial parsing capabilities |
|
src/index.ts |
Code refactoring and formatting improvements |
|
src/index.ts src/options.ts |
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 allow
flag.
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:
The parse
will take an options
object to extend configuration besides simple allow
. We may support inputing a validate
predicate function to choose whether to allow an object / array. The signature of options
may look like this:
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": [{', [])
Inject 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.
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: