-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kbn/server-route-repository] Add zod support (#190244)
This PR adds support for using `zod` as the validation library alongside of `io-ts`. --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
4d0cfdf
commit 0caa6cd
Showing
24 changed files
with
800 additions
and
303 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
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 |
---|---|---|
|
@@ -19,5 +19,6 @@ | |
"@kbn/core-http-browser", | ||
"@kbn/core-http-server", | ||
"@kbn/core", | ||
"@kbn/zod", | ||
] | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
packages/kbn-server-route-repository/src/make_zod_validation_object.test.ts
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { z } from '@kbn/zod'; | ||
import { makeZodValidationObject } from './make_zod_validation_object'; | ||
import { noParamsValidationObject } from './validation_objects'; | ||
|
||
describe('makeZodValidationObject', () => { | ||
it('translate path to params', () => { | ||
const schema = z.object({ | ||
path: z.object({}), | ||
}); | ||
|
||
expect(makeZodValidationObject(schema)).toMatchObject({ | ||
params: expect.anything(), | ||
}); | ||
}); | ||
|
||
it('makes all object types strict', () => { | ||
const schema = z.object({ | ||
path: z.object({}), | ||
query: z.object({}), | ||
body: z.string(), | ||
}); | ||
|
||
const pathStrictSpy = jest.spyOn(schema.shape.path, 'strict'); | ||
const queryStrictSpy = jest.spyOn(schema.shape.query, 'strict'); | ||
|
||
expect(makeZodValidationObject(schema)).toEqual({ | ||
params: pathStrictSpy.mock.results[0].value, | ||
query: queryStrictSpy.mock.results[0].value, | ||
body: schema.shape.body, | ||
}); | ||
}); | ||
|
||
it('sets key to strict empty if schema is missing key', () => { | ||
const schema = z.object({}); | ||
|
||
expect(makeZodValidationObject(schema)).toStrictEqual({ | ||
params: noParamsValidationObject.params, | ||
query: noParamsValidationObject.query, | ||
body: noParamsValidationObject.body, | ||
}); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/kbn-server-route-repository/src/make_zod_validation_object.ts
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { ZodObject, ZodAny } from '@kbn/zod'; | ||
import { ZodParamsObject } from '@kbn/server-route-repository-utils'; | ||
import { noParamsValidationObject } from './validation_objects'; | ||
|
||
export function makeZodValidationObject(params: ZodParamsObject) { | ||
return { | ||
params: params.shape.path ? asStrict(params.shape.path) : noParamsValidationObject.params, | ||
query: params.shape.query ? asStrict(params.shape.query) : noParamsValidationObject.query, | ||
body: params.shape.body ? asStrict(params.shape.body) : noParamsValidationObject.body, | ||
}; | ||
} | ||
|
||
function asStrict(schema: ZodAny) { | ||
if (schema instanceof ZodObject) { | ||
return schema.strict(); | ||
} else { | ||
return schema; | ||
} | ||
} |
Oops, something went wrong.