-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[SavedObjects] Add zod support for schemas and modelVersions
#262683
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
b3cc709
adf6b23
18d6895
9834aa8
d746a05
7dc13cb
64061f6
20a0365
c396cd4
fa663c9
367dfe8
2d64674
ee3ece5
4606285
0fce77a
43f9ed8
add60d5
932e78d
56fc7dc
3d6b69b
4baf59f
05aae0b
dcbb229
6fc5622
9772f0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import { z } from '@kbn/zod'; | ||
| import { schema, type Type } from '@kbn/config-schema'; | ||
| import type { SavedObjectSanitizedDoc } from '@kbn/core-saved-objects-server'; | ||
|
|
||
| // We convert `SavedObjectSanitizedDoc` to its validation schema representation | ||
| // to ensure that we don't forget to keep the schema up-to-date. TS will complain | ||
| // if we update `SavedObjectSanitizedDoc` without making changes below. | ||
| type SavedObjectSanitizedDocSchema = { | ||
| [K in keyof Required<SavedObjectSanitizedDoc>]: Type<SavedObjectSanitizedDoc[K]>; | ||
| }; | ||
|
|
||
| /** | ||
| * Base config-schema schema for a saved object. | ||
| * | ||
| * @internal | ||
| */ | ||
| export const baseConfigSchema = schema.object({ | ||
|
Comment on lines
+14
to
+26
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One base for each schema type, both validated against
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nickofthyme do you still intend to follow through with this PR? The PR seems to be abandoned.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TinaHeiligers Yeah it would be nice. I think zod is the future and we should support it here if we support it in other places like route schemas.
Not abandoned, just no one has had the time review from your team and it's not critical for us so I didn't want to be too much of a bother. I last mentioned it in |
||
| id: schema.string({ minLength: 1 }), | ||
| type: schema.string(), | ||
| references: schema.arrayOf( | ||
| schema.object({ | ||
| name: schema.string(), | ||
| type: schema.string(), | ||
| id: schema.string(), | ||
| }), | ||
| { defaultValue: [], maxSize: 1000 } | ||
| ), | ||
| namespace: schema.maybe(schema.string()), | ||
| namespaces: schema.maybe(schema.arrayOf(schema.string(), { maxSize: 100 })), | ||
| migrationVersion: schema.maybe(schema.recordOf(schema.string(), schema.string())), | ||
| coreMigrationVersion: schema.maybe(schema.string()), | ||
| typeMigrationVersion: schema.maybe(schema.string()), | ||
| updated_at: schema.maybe(schema.string()), | ||
| updated_by: schema.maybe(schema.string()), | ||
| created_at: schema.maybe(schema.string()), | ||
| created_by: schema.maybe(schema.string()), | ||
| version: schema.maybe(schema.string()), | ||
| originId: schema.maybe(schema.string()), | ||
| managed: schema.maybe(schema.boolean()), | ||
| accessControl: schema.maybe( | ||
| schema.object({ | ||
| owner: schema.string(), | ||
| accessMode: schema.oneOf([schema.literal('write_restricted'), schema.literal('default')]), | ||
| }) | ||
| ), | ||
| attributes: schema.recordOf(schema.string(), schema.maybe(schema.any())), | ||
| } satisfies SavedObjectSanitizedDocSchema); | ||
|
|
||
| /** | ||
| * Base Zod schema for a saved object. | ||
| * | ||
| * @internal | ||
| */ | ||
| export const baseZodSchema = z.object({ | ||
| id: z.string().min(1), | ||
| type: z.string(), | ||
| references: z | ||
| .array( | ||
| z.object({ | ||
| name: z.string(), | ||
| type: z.string(), | ||
| id: z.string(), | ||
| }) | ||
| ) | ||
| .max(1000) | ||
| .default([]), | ||
| namespace: z.string().optional(), | ||
| namespaces: z.array(z.string()).max(100).optional(), | ||
| migrationVersion: z.record(z.string(), z.string()).optional(), | ||
| coreMigrationVersion: z.string().optional(), | ||
| typeMigrationVersion: z.string().optional(), | ||
| updated_at: z.string().optional(), | ||
| updated_by: z.string().optional(), | ||
| created_at: z.string().optional(), | ||
| created_by: z.string().optional(), | ||
| version: z.string().optional(), | ||
| originId: z.string().optional(), | ||
| managed: z.boolean().optional(), | ||
| accessControl: z | ||
| .object({ | ||
| owner: z.string(), | ||
| accessMode: z.enum(['write_restricted', 'default']), | ||
| }) | ||
| .optional(), | ||
| attributes: z.record(z.string(), z.any().optional()), | ||
| }) satisfies z.ZodType<SavedObjectSanitizedDoc>; | ||
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.
Return prettified
Errorinstead ofZodError.