From e483dd6c7241aed9642706d307f9ac2e1141bc87 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 11 Feb 2022 10:28:04 +0100 Subject: [PATCH] catalog-model/catalog-backend: Remove usage of deprecated validation methods Signed-off-by: Johan Haals --- .changeset/lazy-jeans-obey.md | 5 ++ .changeset/olive-flies-reflect.md | 6 ++ packages/catalog-model/api-report.md | 12 ---- packages/catalog-model/package.json | 3 +- packages/catalog-model/src/location/index.ts | 5 -- .../catalog-model/src/location/validation.ts | 63 ------------------- plugins/catalog-backend/package.json | 2 +- .../src/service/createRouter.ts | 25 +++++--- plugins/catalog-backend/src/service/util.ts | 17 +++-- 9 files changed, 39 insertions(+), 99 deletions(-) create mode 100644 .changeset/lazy-jeans-obey.md create mode 100644 .changeset/olive-flies-reflect.md delete mode 100644 packages/catalog-model/src/location/validation.ts diff --git a/.changeset/lazy-jeans-obey.md b/.changeset/lazy-jeans-obey.md new file mode 100644 index 0000000000..9124b983dc --- /dev/null +++ b/.changeset/lazy-jeans-obey.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Update internal `Location` validation. diff --git a/.changeset/olive-flies-reflect.md b/.changeset/olive-flies-reflect.md new file mode 100644 index 0000000000..5e61347df7 --- /dev/null +++ b/.changeset/olive-flies-reflect.md @@ -0,0 +1,6 @@ +--- +'@backstage/catalog-model': minor +--- + +Remove deprecated validation methods `analyzeLocationSchema`, `locationSchema` and `locationSpecSchema`. +This functionality was primarily used internally by the `catalog-backend`. diff --git a/packages/catalog-model/api-report.md b/packages/catalog-model/api-report.md index 59eeb98924..c462d8e0b4 100644 --- a/packages/catalog-model/api-report.md +++ b/packages/catalog-model/api-report.md @@ -7,18 +7,12 @@ import { JsonObject } from '@backstage/types'; import { JSONSchema7 } from 'json-schema'; import { JsonValue } from '@backstage/types'; import { SerializedError } from '@backstage/errors'; -import * as yup from 'yup'; // @alpha export interface AlphaEntity extends Entity { status?: EntityStatus; } -// @public @deprecated -export const analyzeLocationSchema: yup.SchemaOf<{ - location: LocationSpec; -}>; - // @public interface ApiEntityV1alpha1 extends Entity { // (undocumented) @@ -354,9 +348,6 @@ export { LocationEntityV1alpha1 }; // @public export const locationEntityV1alpha1Validator: KindValidator; -// @public @deprecated -export const locationSchema: yup.SchemaOf; - // @public export type LocationSpec = { type: string; @@ -364,9 +355,6 @@ export type LocationSpec = { presence?: 'optional' | 'required'; }; -// @public @deprecated -export const locationSpecSchema: yup.SchemaOf; - // @public export function makeValidator(overrides?: Partial): Validators; diff --git a/packages/catalog-model/package.json b/packages/catalog-model/package.json index efe32cccff..56ee513628 100644 --- a/packages/catalog-model/package.json +++ b/packages/catalog-model/package.json @@ -39,8 +39,7 @@ "ajv": "^7.0.3", "json-schema": "^0.4.0", "lodash": "^4.17.21", - "uuid": "^8.0.0", - "yup": "^0.32.9" + "uuid": "^8.0.0" }, "devDependencies": { "@backstage/cli": "^0.13.2-next.0", diff --git a/packages/catalog-model/src/location/index.ts b/packages/catalog-model/src/location/index.ts index ead4a6b564..624c1acd1d 100644 --- a/packages/catalog-model/src/location/index.ts +++ b/packages/catalog-model/src/location/index.ts @@ -25,8 +25,3 @@ export { getEntitySourceLocation, } from './helpers'; export type { Location, LocationSpec } from './types'; -export { - analyzeLocationSchema, - locationSchema, - locationSpecSchema, -} from './validation'; diff --git a/packages/catalog-model/src/location/validation.ts b/packages/catalog-model/src/location/validation.ts deleted file mode 100644 index 2fd692e649..0000000000 --- a/packages/catalog-model/src/location/validation.ts +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2020 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 * as yup from 'yup'; -import { LocationSpec, Location } from './types'; - -/** - * Deprecated. - * - * @public - * @deprecated Use {@link JSONSchema} or validators instead. - */ -export const locationSpecSchema: yup.SchemaOf = yup - .object({ - type: yup.string().required(), - target: yup.string().required(), - presence: yup.mixed().oneOf(['required', 'optional']), - }) - .noUnknown() - .required(); - -/** - * Deprecated. - * - * @public - * @deprecated Use {@link JSONSchema} or validators instead. - */ -export const locationSchema: yup.SchemaOf = yup - .object({ - id: yup.string().required(), - type: yup.string().required(), - target: yup.string().required(), - presence: yup.mixed().oneOf(['required', 'optional']), - }) - .noUnknown() - .required(); - -/** - * Deprecated. - * - * @public - * @deprecated Use {@link JSONSchema} or validators instead. - */ -export const analyzeLocationSchema: yup.SchemaOf<{ location: LocationSpec }> = - yup - .object({ - location: locationSpecSchema, - }) - .noUnknown() - .required(); diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index 7443be73af..4777ad68ff 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -62,7 +62,7 @@ "winston": "^3.2.1", "yaml": "^1.9.2", "yn": "^4.0.0", - "yup": "^0.32.9" + "zod": "^3.11.6" }, "devDependencies": { "@backstage/backend-test-utils": "^0.1.17", diff --git a/plugins/catalog-backend/src/service/createRouter.ts b/plugins/catalog-backend/src/service/createRouter.ts index ab123238c7..0dce765507 100644 --- a/plugins/catalog-backend/src/service/createRouter.ts +++ b/plugins/catalog-backend/src/service/createRouter.ts @@ -15,11 +15,7 @@ */ import { errorHandler } from '@backstage/backend-common'; -import { - analyzeLocationSchema, - locationSpecSchema, - stringifyEntityRef, -} from '@backstage/catalog-model'; +import { stringifyEntityRef } from '@backstage/catalog-model'; import { Config } from '@backstage/config'; import { NotFoundError } from '@backstage/errors'; import express from 'express'; @@ -34,8 +30,13 @@ import { parseEntityPaginationParams, parseEntityTransformParams, } from './request'; -import { disallowReadonlyMode, validateRequestBody } from './util'; +import { + disallowReadonlyMode, + locationSpec, + validateRequestBody, +} from './util'; import { RefreshOptions, LocationService, RefreshService } from './types'; +import { z } from 'zod'; /** * Options used by {@link createRouter}. @@ -165,7 +166,7 @@ export async function createRouter( if (locationService) { router .post('/locations', async (req, res) => { - const input = await validateRequestBody(req, locationSpecSchema); + const location = await validateRequestBody(req, locationSpec); const dryRun = yn(req.query.dryRun, { default: false }); // when in dryRun addLocation is effectively a read operation so we don't @@ -174,7 +175,7 @@ export async function createRouter( disallowReadonlyMode(readonlyEnabled); } - const output = await locationService.createLocation(input, dryRun, { + const output = await locationService.createLocation(location, dryRun, { authorizationToken: getBearerToken(req.header('authorization')), }); res.status(201).json(output); @@ -206,8 +207,12 @@ export async function createRouter( if (locationAnalyzer) { router.post('/analyze-location', async (req, res) => { - const input = await validateRequestBody(req, analyzeLocationSchema); - const output = await locationAnalyzer.analyzeLocation(input); + const body = await validateRequestBody( + req, + z.object({ location: locationSpec }), + ); + const schema = z.object({ location: locationSpec }); + const output = await locationAnalyzer.analyzeLocation(schema.parse(body)); res.status(200).json(output); }); } diff --git a/plugins/catalog-backend/src/service/util.ts b/plugins/catalog-backend/src/service/util.ts index 7cc2d6618a..09f1c2d8cf 100644 --- a/plugins/catalog-backend/src/service/util.ts +++ b/plugins/catalog-backend/src/service/util.ts @@ -17,7 +17,7 @@ import { InputError, NotAllowedError } from '@backstage/errors'; import { Request } from 'express'; import lodash from 'lodash'; -import yup from 'yup'; +import { z } from 'zod'; export async function requireRequestBody(req: Request): Promise { const contentType = req.header('content-type'); @@ -40,19 +40,24 @@ export async function requireRequestBody(req: Request): Promise { return body; } +export const locationSpec = z + .object({ + type: z.string(), + target: z.string(), + presence: z.literal('required').or(z.literal('optional')).optional(), + }) + .strict(); // no unknown keys; + export async function validateRequestBody( req: Request, - schema: yup.AnySchema, + schema: z.Schema, ): Promise { const body = await requireRequestBody(req); - try { - await schema.validate(body, { strict: true }); + return await schema.parse(body); } catch (e) { throw new InputError(`Malformed request: ${e}`); } - - return body as unknown as T; } export function disallowReadonlyMode(readonly: boolean) {