import {z} from 'zod' export type DataQualityRuleKind = 'DataQualityRule'; export type MapLabelKeyString = {[key: string]: string}; export type DataQualityRuleMetadata = {name: string, labels?: MapLabelKeyString | undefined}; export type Cel1 = {celExpression: string, displayName: string, description?: string | undefined, customErrorMessage?: string | undefined, type: 'Cel'}; export type JsonObject = Record; export type JsonSchema1 = {schema: JsonObject, displayName: string, description?: string | undefined, customErrorMessage?: string | undefined, type: 'JsonSchema'}; export type DataQualityRuleSpec = Cel1 | JsonSchema1; export type DataQualityRuleResourceV1 = {apiVersion: string, kind: DataQualityRuleKind, metadata: DataQualityRuleMetadata, spec: DataQualityRuleSpec}; export type UpdateApiPublicDataQualityV1DataQualityRuleArgs = {dryMode?: boolean | undefined, body: DataQualityRuleResourceV1}; export type UpdateApiPublicDataQualityV1DataQualityRuleOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dataQualityRuleKind = z.literal("DataQualityRule"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const dataQualityRuleMetadata = z.object({name: z.string(), labels: mapLabelKeyString.optional()}); export const cel1 = z.object({celExpression: z.string(), displayName: z.string(), description: z.string().optional(), customErrorMessage: z.string().optional(), type: z.literal("Cel")}); export const jsonObject = z.object({}); export const jsonSchema1 = z.object({schema: jsonObject, displayName: z.string(), description: z.string().optional(), customErrorMessage: z.string().optional(), type: z.literal("JsonSchema")}); export const dataQualityRuleSpec = z.discriminatedUnion("type", [cel1, jsonSchema1]); export const dataQualityRuleResourceV1 = z.object({apiVersion: z.string(), kind: dataQualityRuleKind, metadata: dataQualityRuleMetadata, spec: dataQualityRuleSpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const dataQualityRuleApplyResultV1 = z.object({resource: dataQualityRuleResourceV1, upsertResult: upsertResult}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create or update a DataQualityRule * * PUT /public/data-quality/v1/data-quality-rule */ export const updateApiPublicDataQualityV1DataQualityRule = async (args: UpdateApiPublicDataQualityV1DataQualityRuleArgs, options: UpdateApiPublicDataQualityV1DataQualityRuleOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryMode != null) query.append("dryMode", String(args.dryMode)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/data-quality/v1/data-quality-rule${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dataQualityRuleApplyResultV1, await res.json()) };