UNPKG

@bufbuild/protovalidate

Version:

Protocol Buffer Validation for ECMAScript

197 lines (196 loc) 8.08 kB
"use strict"; // Copyright 2024-2025 Buf Technologies, Inc. // // 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. Object.defineProperty(exports, "__esModule", { value: true }); exports.getRuleScalarType = getRuleScalarType; exports.getRuleDescriptor = getRuleDescriptor; exports.getListRules = getListRules; exports.getMapRules = getMapRules; exports.getEnumRules = getEnumRules; exports.getMessageRules = getMessageRules; exports.getScalarRules = getScalarRules; const protobuf_1 = require("@bufbuild/protobuf"); const wkt_1 = require("@bufbuild/protobuf/wkt"); const validate_pb_js_1 = require("./gen/buf/validate/validate_pb.js"); const error_js_1 = require("./error.js"); const messageToRuleType = new Map([ [wkt_1.AnySchema.typeName, "any"], [wkt_1.DurationSchema.typeName, "duration"], [wkt_1.TimestampSchema.typeName, "timestamp"], [wkt_1.FloatValueSchema.typeName, "float"], [wkt_1.DoubleValueSchema.typeName, "double"], [wkt_1.Int32ValueSchema.typeName, "int32"], [wkt_1.Int64ValueSchema.typeName, "int64"], [wkt_1.UInt32ValueSchema.typeName, "uint32"], [wkt_1.UInt64ValueSchema.typeName, "uint64"], [wkt_1.BoolValueSchema.typeName, "bool"], [wkt_1.StringValueSchema.typeName, "string"], [wkt_1.BytesValueSchema.typeName, "bytes"], ]); const scalarToRuleType = new Map([ [protobuf_1.ScalarType.FLOAT, "float"], [protobuf_1.ScalarType.DOUBLE, "double"], [protobuf_1.ScalarType.INT32, "int32"], [protobuf_1.ScalarType.INT64, "int64"], [protobuf_1.ScalarType.UINT32, "uint32"], [protobuf_1.ScalarType.UINT64, "uint64"], [protobuf_1.ScalarType.SINT32, "sint32"], [protobuf_1.ScalarType.SINT64, "sint64"], [protobuf_1.ScalarType.FIXED32, "fixed32"], [protobuf_1.ScalarType.FIXED64, "fixed64"], [protobuf_1.ScalarType.SFIXED32, "sfixed32"], [protobuf_1.ScalarType.SFIXED64, "sfixed64"], [protobuf_1.ScalarType.BOOL, "bool"], [protobuf_1.ScalarType.STRING, "string"], [protobuf_1.ScalarType.BYTES, "bytes"], ]); const ruleMessageTypeToScalarType = new Map([ [validate_pb_js_1.DoubleRulesSchema.typeName, protobuf_1.ScalarType.DOUBLE], [validate_pb_js_1.FloatRulesSchema.typeName, protobuf_1.ScalarType.FLOAT], [validate_pb_js_1.Int64RulesSchema.typeName, protobuf_1.ScalarType.INT64], [validate_pb_js_1.UInt64RulesSchema.typeName, protobuf_1.ScalarType.UINT64], [validate_pb_js_1.Int32RulesSchema.typeName, protobuf_1.ScalarType.INT32], [validate_pb_js_1.Fixed64RulesSchema.typeName, protobuf_1.ScalarType.FIXED64], [validate_pb_js_1.Fixed32RulesSchema.typeName, protobuf_1.ScalarType.FIXED32], [validate_pb_js_1.BoolRulesSchema.typeName, protobuf_1.ScalarType.BOOL], [validate_pb_js_1.BytesRulesSchema.typeName, protobuf_1.ScalarType.BYTES], [validate_pb_js_1.UInt32RulesSchema.typeName, protobuf_1.ScalarType.UINT32], [validate_pb_js_1.SFixed32RulesSchema.typeName, protobuf_1.ScalarType.SFIXED32], [validate_pb_js_1.SFixed64RulesSchema.typeName, protobuf_1.ScalarType.SFIXED64], [validate_pb_js_1.SInt32RulesSchema.typeName, protobuf_1.ScalarType.SINT32], [validate_pb_js_1.SInt64RulesSchema.typeName, protobuf_1.ScalarType.SINT64], ]); /** * Get the ScalarType for one of the buf.validate.*Rules messages. */ function getRuleScalarType(rule) { return ruleMessageTypeToScalarType.get(rule.$typeName); } /** * Get the descriptor for one of the buf.validate.*Rules messages. */ function getRuleDescriptor(typeName) { for (const d of [ validate_pb_js_1.FloatRulesSchema, validate_pb_js_1.DoubleRulesSchema, validate_pb_js_1.Int32RulesSchema, validate_pb_js_1.Int64RulesSchema, validate_pb_js_1.UInt32RulesSchema, validate_pb_js_1.UInt64RulesSchema, validate_pb_js_1.SInt32RulesSchema, validate_pb_js_1.SInt64RulesSchema, validate_pb_js_1.Fixed32RulesSchema, validate_pb_js_1.Fixed64RulesSchema, validate_pb_js_1.SFixed32RulesSchema, validate_pb_js_1.SFixed64RulesSchema, validate_pb_js_1.BoolRulesSchema, validate_pb_js_1.StringRulesSchema, validate_pb_js_1.BytesRulesSchema, validate_pb_js_1.EnumRulesSchema, validate_pb_js_1.RepeatedRulesSchema, validate_pb_js_1.MapRulesSchema, validate_pb_js_1.AnyRulesSchema, validate_pb_js_1.DurationRulesSchema, validate_pb_js_1.TimestampRulesSchema, ]) { if (typeName == d.typeName) { return d; } } throw new Error(`unable to find descriptor for ${typeName}`); } /** * Get buf.validate.RepeatedRules from FieldRules. * Returns a tuple with rules, and path to the rules. * Throws an error if the FieldRules has incompatible rules. */ function getListRules(rulePath, fieldRules, fieldContext) { const listRules = getRulePath(rulePath, "repeated"); return [ getRules(fieldRules, "repeated", fieldContext), listRules, listRules.clone().field(validate_pb_js_1.RepeatedRulesSchema.field.items), ]; } /** * Get buf.validate.MapRules from FieldRules. * Returns a tuple with rules, and path to the rules. * Throws an error if the FieldRules has incompatible rules. */ function getMapRules(rulePath, fieldRules, fieldContext) { const mapRules = getRulePath(rulePath, "map"); return [ getRules(fieldRules, "map", fieldContext), mapRules, mapRules.clone().field(validate_pb_js_1.MapRulesSchema.field.keys), mapRules.clone().field(validate_pb_js_1.MapRulesSchema.field.values), ]; } /** * Get buf.validate.EnumRules from FieldRules. * Returns a tuple with rules, and path to the rules. * Throws an error if the FieldRules has incompatible rules. */ function getEnumRules(rulePath, fieldRules, fieldContext) { return [ getRules(fieldRules, "enum", fieldContext), getRulePath(rulePath, "enum"), ]; } /** * Get buf.validate.*Rules for the given message type from FieldRules. * Returns a tuple with rules, and path to the rules. * Throws an error if the FieldRules has incompatible rules. */ function getMessageRules(descMessage, rulePath, fieldRules, fieldContext) { const type = messageToRuleType.get(descMessage.typeName); return [ getRules(fieldRules, type, fieldContext), getRulePath(rulePath, type), ]; } /** * Get buf.validate.*Rules for the given scalar type from FieldRules. * Returns a tuple with rules, and path to the rules. * Throws an error if the FieldRules has incompatible rules. */ function getScalarRules(scalar, rulePath, fieldRules, fieldContext) { const type = scalarToRuleType.get(scalar); return [ getRules(fieldRules, type, fieldContext), getRulePath(rulePath, type), ]; } function getRulePath(base, type) { if (type == undefined) { return base; } const field = validate_pb_js_1.FieldRulesSchema.fields.find((f) => f.name === type); if (field == undefined) { throw new error_js_1.CompilationError(`cannot find rule "${type}"`); } return base.clone().field(field); } function getRules(fieldRules, want, context) { const got = fieldRules?.type.case; if (fieldRules == undefined || got == undefined) { return undefined; } if (got != want) { throw new error_js_1.CompilationError(want == undefined ? `rule "${got}" cannot be used on ${context.toString()}` : `expected rule "${want}", got "${got}" on ${context.toString()}`); } return fieldRules.type.value; }