0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 10:51:01 +00:00
hono/deno_dist/utils/json.ts
Yusuke Wada 46c6a8de5e
feat(middleware): introduce "built-in" Validator Middleware (#505)
* feat(middleware): introduce built-in Validator Middleware

* fixed type of `req.json()`

* Feat/builtin validator middleware another idea (#508)

* Enable overwriting of query/header data.

* Returns only verified data.

* rename `index.test.ts` to `middleware.test.ts`

* add `removeAdditional` option

* handling error that the JSON body is null

* tidy

* check is it string or not

* implement `isIn`

* enable validation the valies are duplicate

Co-authored-by: Taku Amano <taku@taaas.jp>
2022-09-13 07:54:06 +09:00

34 lines
896 B
TypeScript

export const JSONPathCopy = (src: object, dst: object, path: string) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let srcVal: any = src
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let dstVal: any = dst
const parts = path.split('.')
const length = parts.length
for (let i = 0; i < length && srcVal !== undefined; i++) {
const p = parts[i]
if (p !== '') {
if (typeof srcVal === 'object') {
if (typeof srcVal[p] === 'object') {
dstVal[p] ||= new srcVal[p].constructor()
}
else if (p in srcVal) {
dstVal[p] = srcVal[p]
}
else {
return undefined
}
srcVal = srcVal[p]
dstVal = dstVal[p]
} else {
return undefined
}
}
}
if (typeof srcVal === 'object') {
Object.assign(dstVal, srcVal)
}
return srcVal
}