Files
NapCatQQ/.pnpm-store/v3/files/a5/726c5b58e6932e87215d6826ef5ae7a3bb27e21018cefefa5f6c88e9bb79b25cc131b3fd35864d9768bac166154f144c0c79bbae5c9dffbcc5aa0b0772b630
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

36 lines
1.0 KiB
Plaintext

import type { StandardSchemaV1 } from '@standard-schema/spec'
import { SchemaError } from '@standard-schema/utils'
import type { SchemaType } from './endpointDefinitions'
export class NamedSchemaError extends SchemaError {
constructor(
issues: readonly StandardSchemaV1.Issue[],
public readonly value: any,
public readonly schemaName: `${SchemaType}Schema`,
public readonly _bqMeta: any,
) {
super(issues)
}
}
export const shouldSkip = (
skipSchemaValidation: boolean | SchemaType[] | undefined,
schemaName: SchemaType,
) =>
Array.isArray(skipSchemaValidation)
? skipSchemaValidation.includes(schemaName)
: !!skipSchemaValidation
export async function parseWithSchema<Schema extends StandardSchemaV1>(
schema: Schema,
data: unknown,
schemaName: `${SchemaType}Schema`,
bqMeta: any,
): Promise<StandardSchemaV1.InferOutput<Schema>> {
const result = await schema['~standard'].validate(data)
if (result.issues) {
throw new NamedSchemaError(result.issues, data, schemaName, bqMeta)
}
return result.value
}