UNPKG

@bufbuild/protovalidate

Version:

Protocol Buffer Validation for ECMAScript

130 lines (129 loc) 4.13 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.ignoreListOrMapField = ignoreListOrMapField; exports.ignoreMessageField = ignoreMessageField; exports.ignoreScalarOrEnumField = ignoreScalarOrEnumField; exports.ignoreEnumValue = ignoreEnumValue; exports.ignoreScalarValue = ignoreScalarValue; exports.ignoreMessageValue = ignoreMessageValue; const reflect_1 = require("@bufbuild/protobuf/reflect"); const protobuf_1 = require("@bufbuild/protobuf"); const validate_pb_js_1 = require("./gen/buf/validate/validate_pb.js"); const wkt_1 = require("@bufbuild/protobuf/wkt"); const always = { check() { return true; }, always: true, never: false, }; const never = { check() { return false; }, always: false, never: true, }; function ignoreListOrMapField(field, ignore) { switch (ignore) { case undefined: case validate_pb_js_1.Ignore.UNSPECIFIED: return always; case validate_pb_js_1.Ignore.IF_ZERO_VALUE: return new FieldIsSet(field); case validate_pb_js_1.Ignore.ALWAYS: return never; } } function ignoreMessageField(field, ignore) { switch (ignore) { case undefined: case validate_pb_js_1.Ignore.UNSPECIFIED: case validate_pb_js_1.Ignore.IF_ZERO_VALUE: return new FieldIsSet(field); case validate_pb_js_1.Ignore.ALWAYS: return never; } } function ignoreScalarOrEnumField(field, ignore) { if (ignore == validate_pb_js_1.Ignore.ALWAYS) { return never; } if (field.presence == wkt_1.FeatureSet_FieldPresence.IMPLICIT) { switch (ignore) { case undefined: case validate_pb_js_1.Ignore.UNSPECIFIED: return always; case validate_pb_js_1.Ignore.IF_ZERO_VALUE: return new FieldIsSet(field); } } // field presence EXPLICIT or LEGACY_REQUIRED return new FieldIsSet(field); } function ignoreEnumValue(enu, ignore) { switch (ignore) { case undefined: case validate_pb_js_1.Ignore.UNSPECIFIED: return always; case validate_pb_js_1.Ignore.IF_ZERO_VALUE: return new ScalarNot(protobuf_1.ScalarType.INT32, enu.values[0].number); case validate_pb_js_1.Ignore.ALWAYS: return never; } } function ignoreScalarValue(scalar, ignore) { switch (ignore) { case undefined: case validate_pb_js_1.Ignore.UNSPECIFIED: return always; case validate_pb_js_1.Ignore.IF_ZERO_VALUE: return new ScalarNot(scalar, (0, reflect_1.scalarZeroValue)(scalar, false)); case validate_pb_js_1.Ignore.ALWAYS: return never; } } function ignoreMessageValue(ignore) { switch (ignore) { case undefined: case validate_pb_js_1.Ignore.UNSPECIFIED: case validate_pb_js_1.Ignore.IF_ZERO_VALUE: return always; case validate_pb_js_1.Ignore.ALWAYS: return never; } } class ScalarNot { constructor(scalar, not) { this.scalar = scalar; this.not = not; this.always = false; this.never = false; } check(val) { return !(0, reflect_1.scalarEquals)(this.scalar, this.not, val); } } class FieldIsSet { constructor(field) { this.field = field; this.always = false; this.never = false; } check(val) { return val.isSet(this.field); } }