forked from honojs/hono
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validator): add support for JSON array path validation (honojs#563)
* refactor(JSONPath): improve typing of JSONPath * chore(vscode-settings): add deno.enable=false * fix(validator): add null to type Type def * feat(JSON-Path): add support for array JSONPath-Plus syntax * fix(validator): update isRequired to pass valid bool types * test: update tests for isRequired validator * feat(validator): add support for JSON array path validation * chore(deno): denoify array support changes * fix(validator): type check all vals in array * chore(deno): denoify changes * test(validator): add tests for array type checking * fix(validator): change JSONPrimative to JSONPrimitive * refactor(json): More compatible with https://jsonpath.com/. * implementation of `asArray` * fix(validator): update JSONPath implementation and add isArray check in validation * fix(validator): fix typing errors on SchemaToProp * Revert "fix(validator): fix typing errors on SchemaToProp" This reverts commit b8ddef8. * fix(validator): fix SchemaToProp error for VTypeArrays * chore(deno): denoify Co-authored-by: Taku Amano <[email protected]> Co-authored-by: Yusuke Wada <[email protected]>
- Loading branch information
1 parent
13ce2ee
commit 9a0389e
Showing
10 changed files
with
626 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
{ | ||
"eslint.validate": [ | ||
"javascript", | ||
"javascriptreact", | ||
"typescript", | ||
"typescriptreact" | ||
], | ||
"deno.enable": false, | ||
"eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"], | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
export const JSONPath = (data: object, path: string) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let val: any = data | ||
const parts = path.split('.') | ||
export type JSONPrimitive = string | boolean | number | null | undefined | ||
export type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[] | ||
export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject } | ||
export type JSONValue = JSONObject | JSONArray | JSONPrimitive | ||
|
||
const JSONPathInternal = (data: JSONValue, parts: string[]): JSONValue => { | ||
const length = parts.length | ||
for (let i = 0; i < length && val !== undefined; i++) { | ||
for (let i = 0; i < length && data !== undefined; i++) { | ||
const p = parts[i] | ||
if (p !== '') { | ||
if (typeof val === 'object') { | ||
val = val[p] | ||
} else { | ||
val = undefined | ||
} | ||
if (p === '') { | ||
continue | ||
} | ||
|
||
if (typeof data !== 'object' || data === null) { | ||
return undefined | ||
} | ||
|
||
if (p === '*') { | ||
const restParts = parts.slice(i + 1) | ||
const values = Object.values(data).map((v) => JSONPathInternal(v, restParts)) | ||
return restParts.indexOf('*') === -1 ? values : values.flat() | ||
} else { | ||
data = (data as JSONObject)[p] // `data` may be an array, but accessing it as an object yields the same result. | ||
} | ||
} | ||
return data | ||
} | ||
|
||
export const JSONPath = (data: JSONObject, path: string) => { | ||
try { | ||
return JSONPathInternal(data, path.replace(/\[(.*?)\]/g, '.$1').split(/\./)) | ||
} catch (e) { | ||
return undefined | ||
} | ||
return val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.