diff --git a/plugins/scaffolder/api-report.md b/plugins/scaffolder/api-report.md index 69512e7af0..e56b280c05 100644 --- a/plugins/scaffolder/api-report.md +++ b/plugins/scaffolder/api-report.md @@ -35,7 +35,6 @@ import { TaskStep } from '@backstage/plugin-scaffolder-common'; import { TemplateEntityV1beta3 } from '@backstage/plugin-scaffolder-common'; import { UIOptionsType } from '@rjsf/utils'; import { UiSchema } from '@rjsf/utils'; -import { z } from 'zod'; // @alpha export function createNextScaffolderFieldExtension< @@ -91,33 +90,19 @@ export const EntityPickerFieldExtension: FieldExtensionComponent< >; // @public -export type EntityPickerUiOptions = z.infer; +export type EntityPickerUiOptions = + typeof EntityPickerUiOptionsSchema.schemaType; // @public (undocumented) -export const EntityPickerUiOptionsSchema: z.ZodObject< - { - allowedKinds: z.ZodOptional>; - defaultKind: z.ZodOptional; - allowArbitraryValues: z.ZodOptional; - defaultNamespace: z.ZodOptional< - z.ZodUnion<[z.ZodString, z.ZodLiteral]> - >; - }, - 'strip', - z.ZodTypeAny, - { +export const EntityPickerUiOptionsSchema: { + jsonSchema: JSONSchema7; + schemaType: { defaultKind?: string | undefined; defaultNamespace?: string | false | undefined; allowedKinds?: string[] | undefined; allowArbitraryValues?: boolean | undefined; - }, - { - defaultKind?: string | undefined; - defaultNamespace?: string | false | undefined; - allowedKinds?: string[] | undefined; - allowArbitraryValues?: boolean | undefined; - } ->; + }; +}; // @public export const EntityTagsPickerFieldExtension: FieldExtensionComponent< @@ -130,30 +115,18 @@ export const EntityTagsPickerFieldExtension: FieldExtensionComponent< >; // @public -export type EntityTagsPickerUiOptions = z.infer< - typeof EntityTagsPickerUiOptionsSchema ->; +export type EntityTagsPickerUiOptions = + typeof EntityTagsPickerUiOptionsSchema.schemaType; // @public (undocumented) -export const EntityTagsPickerUiOptionsSchema: z.ZodObject< - { - kinds: z.ZodOptional>; - showCounts: z.ZodOptional; - helperText: z.ZodOptional; - }, - 'strip', - z.ZodTypeAny, - { +export const EntityTagsPickerUiOptionsSchema: { + jsonSchema: JSONSchema7; + schemaType: { showCounts?: boolean | undefined; kinds?: string[] | undefined; helperText?: string | undefined; - }, - { - showCounts?: boolean | undefined; - kinds?: string[] | undefined; - helperText?: string | undefined; - } ->; + }; +}; // @public export type FieldExtensionComponent<_TReturnValue, _TInputProps> = () => null; @@ -288,35 +261,19 @@ export const OwnedEntityPickerFieldExtension: FieldExtensionComponent< >; // @public -export type OwnedEntityPickerUiOptions = z.infer< - typeof OwnedEntityPickerUiOptionsSchema ->; +export type OwnedEntityPickerUiOptions = + typeof OwnedEntityPickerUiOptionsSchema.schemaType; // @public (undocumented) -export const OwnedEntityPickerUiOptionsSchema: z.ZodObject< - { - allowedKinds: z.ZodOptional>; - defaultKind: z.ZodOptional; - allowArbitraryValues: z.ZodOptional; - defaultNamespace: z.ZodOptional< - z.ZodUnion<[z.ZodString, z.ZodLiteral]> - >; - }, - 'strip', - z.ZodTypeAny, - { +export const OwnedEntityPickerUiOptionsSchema: { + jsonSchema: JSONSchema7; + schemaType: { defaultKind?: string | undefined; defaultNamespace?: string | false | undefined; allowedKinds?: string[] | undefined; allowArbitraryValues?: boolean | undefined; - }, - { - defaultKind?: string | undefined; - defaultNamespace?: string | false | undefined; - allowedKinds?: string[] | undefined; - allowArbitraryValues?: boolean | undefined; - } ->; + }; +}; // @public export const OwnerPickerFieldExtension: FieldExtensionComponent< @@ -329,30 +286,17 @@ export const OwnerPickerFieldExtension: FieldExtensionComponent< >; // @public -export type OwnerPickerUiOptions = z.infer; +export type OwnerPickerUiOptions = typeof OwnerPickerUiOptionsSchema.schemaType; // @public (undocumented) -export const OwnerPickerUiOptionsSchema: z.ZodObject< - { - allowedKinds: z.ZodOptional>>; - allowArbitraryValues: z.ZodOptional; - defaultNamespace: z.ZodOptional< - z.ZodUnion<[z.ZodString, z.ZodLiteral]> - >; - }, - 'strip', - z.ZodTypeAny, - { +export const OwnerPickerUiOptionsSchema: { + jsonSchema: JSONSchema7; + schemaType: { defaultNamespace?: string | false | undefined; allowedKinds?: string[] | undefined; allowArbitraryValues?: boolean | undefined; - }, - { - defaultNamespace?: string | false | undefined; - allowedKinds?: string[] | undefined; - allowArbitraryValues?: boolean | undefined; - } ->; + }; +}; // @public export const repoPickerValidation: ( @@ -389,81 +333,13 @@ export const RepoUrlPickerFieldExtension: FieldExtensionComponent< >; // @public -export type RepoUrlPickerUiOptions = z.infer< - typeof RepoUrlPickerUiOptionsSchema ->; +export type RepoUrlPickerUiOptions = + typeof RepoUrlPickerUiOptionsSchema.schemaType; // @public (undocumented) -export const RepoUrlPickerUiOptionsSchema: z.ZodObject< - { - allowedHosts: z.ZodOptional>; - allowedOrganizations: z.ZodOptional>; - allowedOwners: z.ZodOptional>; - allowedRepos: z.ZodOptional>; - requestUserCredentials: z.ZodOptional< - z.ZodObject< - { - secretsKey: z.ZodString; - additionalScopes: z.ZodOptional< - z.ZodObject< - { - gerrit: z.ZodOptional>; - github: z.ZodOptional>; - gitlab: z.ZodOptional>; - bitbucket: z.ZodOptional>; - azure: z.ZodOptional>; - }, - 'strip', - z.ZodTypeAny, - { - azure?: string[] | undefined; - github?: string[] | undefined; - gitlab?: string[] | undefined; - bitbucket?: string[] | undefined; - gerrit?: string[] | undefined; - }, - { - azure?: string[] | undefined; - github?: string[] | undefined; - gitlab?: string[] | undefined; - bitbucket?: string[] | undefined; - gerrit?: string[] | undefined; - } - > - >; - }, - 'strip', - z.ZodTypeAny, - { - additionalScopes?: - | { - azure?: string[] | undefined; - github?: string[] | undefined; - gitlab?: string[] | undefined; - bitbucket?: string[] | undefined; - gerrit?: string[] | undefined; - } - | undefined; - secretsKey: string; - }, - { - additionalScopes?: - | { - azure?: string[] | undefined; - github?: string[] | undefined; - gitlab?: string[] | undefined; - bitbucket?: string[] | undefined; - gerrit?: string[] | undefined; - } - | undefined; - secretsKey: string; - } - > - >; - }, - 'strip', - z.ZodTypeAny, - { +export const RepoUrlPickerUiOptionsSchema: { + jsonSchema: JSONSchema7; + schemaType: { allowedOwners?: string[] | undefined; allowedOrganizations?: string[] | undefined; allowedRepos?: string[] | undefined; @@ -482,28 +358,8 @@ export const RepoUrlPickerUiOptionsSchema: z.ZodObject< secretsKey: string; } | undefined; - }, - { - allowedOwners?: string[] | undefined; - allowedOrganizations?: string[] | undefined; - allowedRepos?: string[] | undefined; - allowedHosts?: string[] | undefined; - requestUserCredentials?: - | { - additionalScopes?: - | { - azure?: string[] | undefined; - github?: string[] | undefined; - gitlab?: string[] | undefined; - bitbucket?: string[] | undefined; - gerrit?: string[] | undefined; - } - | undefined; - secretsKey: string; - } - | undefined; - } ->; + }; +}; // @public (undocumented) export const rootRouteRef: RouteRef; diff --git a/plugins/scaffolder/src/components/fields/EntityNamePicker/schema.ts b/plugins/scaffolder/src/components/fields/EntityNamePicker/schema.ts index 45aa4502ce..83e477b6aa 100644 --- a/plugins/scaffolder/src/components/fields/EntityNamePicker/schema.ts +++ b/plugins/scaffolder/src/components/fields/EntityNamePicker/schema.ts @@ -13,18 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { JSONSchema7 } from 'json-schema'; import { z } from 'zod'; -import zodToJsonSchema from 'zod-to-json-schema'; +import { makeJsonSchemaFromZod } from '../utils'; -const EntityNamePickerReturnValueSchema = z.string(); +const EntityNamePickerReturnValueSchema = makeJsonSchemaFromZod(z.string()); -export type EntityNamePickerReturnValue = z.infer< - typeof EntityNamePickerReturnValueSchema ->; +export type EntityNamePickerReturnValue = + typeof EntityNamePickerReturnValueSchema.schemaType; export const EntityNamePickerSchema = { - returnValue: zodToJsonSchema( - EntityNamePickerReturnValueSchema, - ) as JSONSchema7, + returnValue: EntityNamePickerReturnValueSchema.jsonSchema, }; diff --git a/plugins/scaffolder/src/components/fields/EntityPicker/schema.ts b/plugins/scaffolder/src/components/fields/EntityPicker/schema.ts index 5f7d66fc4f..3e05264a14 100644 --- a/plugins/scaffolder/src/components/fields/EntityPicker/schema.ts +++ b/plugins/scaffolder/src/components/fields/EntityPicker/schema.ts @@ -13,37 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { JSONSchema7 } from 'json-schema'; import { z } from 'zod'; -import zodToJsonSchema from 'zod-to-json-schema'; +import { makeJsonSchemaFromZod } from '../utils'; /** * @public */ -export const EntityPickerUiOptionsSchema = z.object({ - allowedKinds: z - .array(z.string()) - .optional() - .describe('List of kinds of entities to derive options from'), - defaultKind: z - .string() - .optional() - .describe( - 'The default entity kind. Options of this kind will not be prefixed.', - ), - allowArbitraryValues: z - .boolean() - .optional() - .describe('Whether to allow arbitrary user input. Defaults to true'), - defaultNamespace: z - .union([z.string(), z.literal(false)]) - .optional() - .describe( - 'The default namespace. Options with this namespace will not be prefixed.', - ), -}); +export const EntityPickerUiOptionsSchema = makeJsonSchemaFromZod( + z.object({ + allowedKinds: z + .array(z.string()) + .optional() + .describe('List of kinds of entities to derive options from'), + defaultKind: z + .string() + .optional() + .describe( + 'The default entity kind. Options of this kind will not be prefixed.', + ), + allowArbitraryValues: z + .boolean() + .optional() + .describe('Whether to allow arbitrary user input. Defaults to true'), + defaultNamespace: z + .union([z.string(), z.literal(false)]) + .optional() + .describe( + 'The default namespace. Options with this namespace will not be prefixed.', + ), + }), +); -const EntityPickerReturnValueSchema = z.string(); +const EntityPickerReturnValueSchema = makeJsonSchemaFromZod(z.string()); /** * The input props that can be specified under `ui:options` for the @@ -51,13 +52,13 @@ const EntityPickerReturnValueSchema = z.string(); * * @public */ -export type EntityPickerUiOptions = z.infer; +export type EntityPickerUiOptions = + typeof EntityPickerUiOptionsSchema.schemaType; -export type EntityPickerReturnValue = z.infer< - typeof EntityPickerReturnValueSchema ->; +export type EntityPickerReturnValue = + typeof EntityPickerReturnValueSchema.schemaType; export const EntityPickerSchema = { - uiOptions: zodToJsonSchema(EntityPickerUiOptionsSchema) as JSONSchema7, - returnValue: zodToJsonSchema(EntityPickerReturnValueSchema) as JSONSchema7, + uiOptions: EntityPickerUiOptionsSchema.jsonSchema, + returnValue: EntityPickerReturnValueSchema.jsonSchema, }; diff --git a/plugins/scaffolder/src/components/fields/EntityTagsPicker/schema.ts b/plugins/scaffolder/src/components/fields/EntityTagsPicker/schema.ts index ff477903d7..e08918f838 100644 --- a/plugins/scaffolder/src/components/fields/EntityTagsPicker/schema.ts +++ b/plugins/scaffolder/src/components/fields/EntityTagsPicker/schema.ts @@ -13,26 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { JSONSchema7 } from 'json-schema'; import { z } from 'zod'; -import zodToJsonSchema from 'zod-to-json-schema'; +import { makeJsonSchemaFromZod } from '../utils'; /** * @public */ -export const EntityTagsPickerUiOptionsSchema = z.object({ - kinds: z - .array(z.string()) - .optional() - .describe('List of kinds of entities to derive tags from'), - showCounts: z - .boolean() - .optional() - .describe('Whether to show usage counts per tag'), - helperText: z.string().optional().describe('Helper text to display'), -}); +export const EntityTagsPickerUiOptionsSchema = makeJsonSchemaFromZod( + z.object({ + kinds: z + .array(z.string()) + .optional() + .describe('List of kinds of entities to derive tags from'), + showCounts: z + .boolean() + .optional() + .describe('Whether to show usage counts per tag'), + helperText: z.string().optional().describe('Helper text to display'), + }), +); -const EntityTagsPickerReturnValueSchema = z.array(z.string()); +const EntityTagsPickerReturnValueSchema = makeJsonSchemaFromZod( + z.array(z.string()), +); /** * The input props that can be specified under `ui:options` for the @@ -40,17 +43,13 @@ const EntityTagsPickerReturnValueSchema = z.array(z.string()); * * @public */ -export type EntityTagsPickerUiOptions = z.infer< - typeof EntityTagsPickerUiOptionsSchema ->; +export type EntityTagsPickerUiOptions = + typeof EntityTagsPickerUiOptionsSchema.schemaType; -export type EntityTagsPickerReturnValue = z.infer< - typeof EntityTagsPickerReturnValueSchema ->; +export type EntityTagsPickerReturnValue = + typeof EntityTagsPickerReturnValueSchema.schemaType; export const EntityTagsPickerSchema = { - uiOptions: zodToJsonSchema(EntityTagsPickerUiOptionsSchema) as JSONSchema7, - returnValue: zodToJsonSchema( - EntityTagsPickerReturnValueSchema, - ) as JSONSchema7, + uiOptions: EntityTagsPickerUiOptionsSchema.jsonSchema, + returnValue: EntityTagsPickerReturnValueSchema.jsonSchema, }; diff --git a/plugins/scaffolder/src/components/fields/OwnedEntityPicker/schema.ts b/plugins/scaffolder/src/components/fields/OwnedEntityPicker/schema.ts index c260fae3cd..e5bfcd9709 100644 --- a/plugins/scaffolder/src/components/fields/OwnedEntityPicker/schema.ts +++ b/plugins/scaffolder/src/components/fields/OwnedEntityPicker/schema.ts @@ -13,37 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { JSONSchema7 } from 'json-schema'; import { z } from 'zod'; -import zodToJsonSchema from 'zod-to-json-schema'; +import { makeJsonSchemaFromZod } from '../utils'; /** * @public */ -export const OwnedEntityPickerUiOptionsSchema = z.object({ - allowedKinds: z - .array(z.string()) - .optional() - .describe('List of kinds of entities to derive options from'), - defaultKind: z - .string() - .optional() - .describe( - 'The default entity kind. Options of this kind will not be prefixed.', - ), - allowArbitraryValues: z - .boolean() - .optional() - .describe('Whether to allow arbitrary user input. Defaults to true'), - defaultNamespace: z - .union([z.string(), z.literal(false)]) - .optional() - .describe( - 'The default namespace. Options with this namespace will not be prefixed.', - ), -}); +export const OwnedEntityPickerUiOptionsSchema = makeJsonSchemaFromZod( + z.object({ + allowedKinds: z + .array(z.string()) + .optional() + .describe('List of kinds of entities to derive options from'), + defaultKind: z + .string() + .optional() + .describe( + 'The default entity kind. Options of this kind will not be prefixed.', + ), + allowArbitraryValues: z + .boolean() + .optional() + .describe('Whether to allow arbitrary user input. Defaults to true'), + defaultNamespace: z + .union([z.string(), z.literal(false)]) + .optional() + .describe( + 'The default namespace. Options with this namespace will not be prefixed.', + ), + }), +); -const OwnedEntityPickerReturnValueSchema = z.string(); +const OwnedEntityPickerReturnValueSchema = makeJsonSchemaFromZod(z.string()); /** * The input props that can be specified under `ui:options` for the @@ -51,17 +52,13 @@ const OwnedEntityPickerReturnValueSchema = z.string(); * * @public */ -export type OwnedEntityPickerUiOptions = z.infer< - typeof OwnedEntityPickerUiOptionsSchema ->; +export type OwnedEntityPickerUiOptions = + typeof OwnedEntityPickerUiOptionsSchema.schemaType; -export type OwnedEntityPickerReturnValue = z.infer< - typeof OwnedEntityPickerReturnValueSchema ->; +export type OwnedEntityPickerReturnValue = + typeof OwnedEntityPickerReturnValueSchema.schemaType; export const OwnedEntityPickerSchema = { - uiOptions: zodToJsonSchema(OwnedEntityPickerUiOptionsSchema) as JSONSchema7, - returnValue: zodToJsonSchema( - OwnedEntityPickerReturnValueSchema, - ) as JSONSchema7, + uiOptions: OwnedEntityPickerUiOptionsSchema.jsonSchema, + returnValue: OwnedEntityPickerReturnValueSchema.jsonSchema, }; diff --git a/plugins/scaffolder/src/components/fields/OwnerPicker/schema.ts b/plugins/scaffolder/src/components/fields/OwnerPicker/schema.ts index 634a39ddee..ccfe453651 100644 --- a/plugins/scaffolder/src/components/fields/OwnerPicker/schema.ts +++ b/plugins/scaffolder/src/components/fields/OwnerPicker/schema.ts @@ -13,34 +13,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { JSONSchema7 } from 'json-schema'; import { z } from 'zod'; -import zodToJsonSchema from 'zod-to-json-schema'; +import { makeJsonSchemaFromZod } from '../utils'; /** * @public */ -export const OwnerPickerUiOptionsSchema = z.object({ - allowedKinds: z - .array(z.string()) - .default(['Group', 'User']) - .optional() - .describe( - 'List of kinds of entities to derive options from. Defaults to Group and User', - ), - allowArbitraryValues: z - .boolean() - .optional() - .describe('Whether to allow arbitrary user input. Defaults to true'), - defaultNamespace: z - .union([z.string(), z.literal(false)]) - .optional() - .describe( - 'The default namespace. Options with this namespace will not be prefixed.', - ), -}); +export const OwnerPickerUiOptionsSchema = makeJsonSchemaFromZod( + z.object({ + allowedKinds: z + .array(z.string()) + .default(['Group', 'User']) + .optional() + .describe( + 'List of kinds of entities to derive options from. Defaults to Group and User', + ), + allowArbitraryValues: z + .boolean() + .optional() + .describe('Whether to allow arbitrary user input. Defaults to true'), + defaultNamespace: z + .union([z.string(), z.literal(false)]) + .optional() + .describe( + 'The default namespace. Options with this namespace will not be prefixed.', + ), + }), +); -const OwnerPickerReturnValueSchema = z.string(); +const OwnerPickerReturnValueSchema = makeJsonSchemaFromZod(z.string()); /** * The input props that can be specified under `ui:options` for the @@ -48,13 +49,12 @@ const OwnerPickerReturnValueSchema = z.string(); * * @public */ -export type OwnerPickerUiOptions = z.infer; +export type OwnerPickerUiOptions = typeof OwnerPickerUiOptionsSchema.schemaType; -export type OwnerPickerReturnValue = z.infer< - typeof OwnerPickerReturnValueSchema ->; +export type OwnerPickerReturnValue = + typeof OwnerPickerReturnValueSchema.schemaType; export const OwnerPickerSchema = { - uiOptions: zodToJsonSchema(OwnerPickerUiOptionsSchema) as JSONSchema7, - returnValue: zodToJsonSchema(OwnerPickerReturnValueSchema) as JSONSchema7, + uiOptions: OwnerPickerUiOptionsSchema.jsonSchema, + returnValue: OwnerPickerReturnValueSchema.jsonSchema, }; diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/schema.ts b/plugins/scaffolder/src/components/fields/RepoUrlPicker/schema.ts index b870747fd4..98d9f76a9c 100644 --- a/plugins/scaffolder/src/components/fields/RepoUrlPicker/schema.ts +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/schema.ts @@ -13,70 +13,71 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { JSONSchema7 } from 'json-schema'; import { z } from 'zod'; -import zodToJsonSchema from 'zod-to-json-schema'; +import { makeJsonSchemaFromZod } from '../utils'; /** * @public */ -export const RepoUrlPickerUiOptionsSchema = z.object({ - allowedHosts: z - .array(z.string()) - .optional() - .describe('List of allowed SCM platform hosts'), - allowedOrganizations: z - .array(z.string()) - .optional() - .describe('List of allowed organizations in the given SCM platform'), - allowedOwners: z - .array(z.string()) - .optional() - .describe('List of allowed owners in the given SCM platform'), - allowedRepos: z - .array(z.string()) - .optional() - .describe('List of allowed repos in the given SCM platform'), - requestUserCredentials: z - .object({ - secretsKey: z - .string() - .describe( - 'Key used within the template secrets context to store the credential', - ), - additionalScopes: z - .object({ - gerrit: z - .array(z.string()) - .optional() - .describe('Additional Gerrit scopes to request'), - github: z - .array(z.string()) - .optional() - .describe('Additional GitHub scopes to request'), - gitlab: z - .array(z.string()) - .optional() - .describe('Additional GitLab scopes to request'), - bitbucket: z - .array(z.string()) - .optional() - .describe('Additional BitBucket scopes to request'), - azure: z - .array(z.string()) - .optional() - .describe('Additional Azure scopes to request'), - }) - .optional() - .describe('Additional permission scopes to request'), - }) - .optional() - .describe( - 'If defined will request user credentials to auth against the given SCM platform', - ), -}); +export const RepoUrlPickerUiOptionsSchema = makeJsonSchemaFromZod( + z.object({ + allowedHosts: z + .array(z.string()) + .optional() + .describe('List of allowed SCM platform hosts'), + allowedOrganizations: z + .array(z.string()) + .optional() + .describe('List of allowed organizations in the given SCM platform'), + allowedOwners: z + .array(z.string()) + .optional() + .describe('List of allowed owners in the given SCM platform'), + allowedRepos: z + .array(z.string()) + .optional() + .describe('List of allowed repos in the given SCM platform'), + requestUserCredentials: z + .object({ + secretsKey: z + .string() + .describe( + 'Key used within the template secrets context to store the credential', + ), + additionalScopes: z + .object({ + gerrit: z + .array(z.string()) + .optional() + .describe('Additional Gerrit scopes to request'), + github: z + .array(z.string()) + .optional() + .describe('Additional GitHub scopes to request'), + gitlab: z + .array(z.string()) + .optional() + .describe('Additional GitLab scopes to request'), + bitbucket: z + .array(z.string()) + .optional() + .describe('Additional BitBucket scopes to request'), + azure: z + .array(z.string()) + .optional() + .describe('Additional Azure scopes to request'), + }) + .optional() + .describe('Additional permission scopes to request'), + }) + .optional() + .describe( + 'If defined will request user credentials to auth against the given SCM platform', + ), + }), +); -const RepoUrlPickerReturnValueSchema = z.string(); +const RepoUrlPickerReturnValueSchema = makeJsonSchemaFromZod(z.string()); /** * The input props that can be specified under `ui:options` for the @@ -84,18 +85,16 @@ const RepoUrlPickerReturnValueSchema = z.string(); * * @public */ -export type RepoUrlPickerUiOptions = z.infer< - typeof RepoUrlPickerUiOptionsSchema ->; +export type RepoUrlPickerUiOptions = + typeof RepoUrlPickerUiOptionsSchema.schemaType; -export type RepoUrlPickerReturnValue = z.infer< - typeof RepoUrlPickerReturnValueSchema ->; +export type RepoUrlPickerReturnValue = + typeof RepoUrlPickerReturnValueSchema.schemaType; // NOTE: There is a bug with this failing validation in the custom field explorer due // to https://github.com/rjsf-team/react-jsonschema-form/issues/675 even if // requestUserCredentials is not defined export const RepoUrlPickerSchema = { - uiOptions: zodToJsonSchema(RepoUrlPickerUiOptionsSchema) as JSONSchema7, - returnValue: zodToJsonSchema(RepoUrlPickerReturnValueSchema) as JSONSchema7, + uiOptions: RepoUrlPickerUiOptionsSchema.jsonSchema, + returnValue: RepoUrlPickerReturnValueSchema.jsonSchema, }; diff --git a/plugins/scaffolder/src/components/fields/utils.ts b/plugins/scaffolder/src/components/fields/utils.ts new file mode 100644 index 0000000000..b3ebf1e1b4 --- /dev/null +++ b/plugins/scaffolder/src/components/fields/utils.ts @@ -0,0 +1,34 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { JSONSchema7 } from 'json-schema'; +import { z } from 'zod'; +import zodToJsonSchema from 'zod-to-json-schema'; + +/** + * Utility function to convert zod schemas to JSON schemas with + * type inference extraction that abstracts away zod typings + */ +export function makeJsonSchemaFromZod( + schema: T, +): { + jsonSchema: JSONSchema7; + schemaType: T extends z.ZodType ? I : never; +} { + return { + jsonSchema: zodToJsonSchema(schema) as JSONSchema7, + schemaType: null as any, + }; +}