UNPKG

@bufbuild/protovalidate

Version:

Protocol Buffer Validation for ECMAScript

91 lines (90 loc) 3.36 kB
import { type Path } from "@bufbuild/protobuf/reflect"; import { type FieldPath } from "./gen/buf/validate/validate_pb.js"; import { type DescMessage, type Registry } from "@bufbuild/protobuf"; /** * A CompilationError is raised if a CEL expression cannot be compiled, or if * invalid standard rules are applied. */ export declare class CompilationError extends Error { name: string; constructor(message: string, options?: { cause?: unknown; }); } /** * A RuntimeError is raised if a CEL expression errors or returns an * unexpected value, or if the schema and message provided to the * validator mismatch. */ export declare class RuntimeError extends Error { name: string; constructor(message: string, options?: { cause?: unknown; }); } /** * A ValidationError is raised if one or more rule violations were * detected. */ export declare class ValidationError extends Error { name: "ValidationError"; readonly violations: Violation[]; constructor(violations: Violation[]); } /** * Violation represents a single instance where a validation rule was not met. * It provides information about the field that caused the violation, the * specific unfulfilled rule, and a human-readable error message. */ export declare class Violation { /** * A human-readable error message that describes the nature of the violation. * * This can be the default error message from the violated `Rule`, or it * can be a custom message that gives more context about the violation. */ message: string; /** * The unique identifier of the `Rule` that was not fulfilled. * This is the same `id` that was specified in the `Rule` message, * allowing easy tracing of which rule was violated. */ ruleId: string; /** * A machine-readable path to the field that failed validation. * * This could be a nested field, in which case the path will include all the * parent fields leading to the actual field that caused the violation. */ field: Path; /** * A machine-readable path that points to the specific rule that failed * validation. * * This will be a nested field starting from the FieldRules of the field * that failed validation. For custom rules, this will provide the path * of the rule, e.g. `cel[0]`. */ rule: Path; /** * Indicates whether the violation was caused by a map key, rather than a value. */ forKey: boolean; constructor(message: string, ruleId: string, field: Path, rule: Path, forKey: boolean); toString(): string; } /** * Convert an array of Violation[] to the Protobuf message buf.validate.Violations. */ export declare function violationsToProto(violation: Violation[]): import("./gen/buf/validate/validate_pb.js").Violations; /** * Convert a Violation to the Protobuf message buf.validate.Violation. */ export declare function violationToProto(violation: Violation): import("./gen/buf/validate/validate_pb.js").Violation; /** * Convert a Protobuf message buf.validate.FieldPath to a Path. * * Raises an error if the Protobuf message cannot be converted because of a schema * mismatch, or because it is invalid. */ export declare function pathFromViolationProto(schema: DescMessage, proto: FieldPath, registry?: Registry): Path;