Debian Codegraph @

node/@bufbuild/protobuf/dist/esm/reflect/reflect-check.js

raw
1 // Copyright 2021-2026 Buf Technologies, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 import { ScalarType, } from "../descriptors.js";
15 import { isMessage } from "../is-message.js";
16 import { FieldError } from "./error.js";
17 import { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
18 import { FLOAT32_MAX, FLOAT32_MIN, INT32_MAX, INT32_MIN, UINT32_MAX, } from "../wire/binary-encoding.js";
19 import { getTextEncoding } from "../wire/text-encoding.js";
20 import { protoInt64 } from "../proto-int64.js";
21 /**
22 * Check whether the given field value is valid for the reflect API.
23 */
24 export function checkField(field, value) {
25 const check = field.fieldKind == "list"
26 ? isReflectList(value, field)
27 : field.fieldKind == "map"
28 ? isReflectMap(value, field)
29 : checkSingular(field, value);
30 if (check === true) {
31 return undefined;
32 }
33 let reason;
34 switch (field.fieldKind) {
35 case "list":
36 reason = `expected ${formatReflectList(field)}, got ${formatVal(value)}`;
37 break;
38 case "map":
39 reason = `expected ${formatReflectMap(field)}, got ${formatVal(value)}`;
40 break;
41 default: {
42 reason = reasonSingular(field, value, check);
43 }
44 }
45 return new FieldError(field, reason);
46 }
47 /**
48 * Check whether the given list item is valid for the reflect API.
49 */
50 export function checkListItem(field, index, value) {
51 const check = checkSingular(field, value);
52 if (check !== true) {
53 return new FieldError(field, `list item #${index + 1}: ${reasonSingular(field, value, check)}`);
54 }
55 return undefined;
56 }
57 /**
58 * Check whether the given map key and value are valid for the reflect API.
59 */
60 export function checkMapEntry(field, key, value) {
61 const checkKey = checkScalarValue(field.mapKey)(key);
62 if (checkKey !== true) {
63 return new FieldError(field, `invalid map key: ${reasonSingular({ scalar: field.mapKey }, key, checkKey)}`);
64 }
65 const checkVal = checkSingular(field, value);
66 if (checkVal !== true) {
67 return new FieldError(field, `map entry ${formatVal(key)}: ${reasonSingular(field, value, checkVal)}`);
68 }
69 return undefined;
70 }
71 function checkSingular(field, value) {
72 if (field.scalar !== undefined) {
73 return checkScalarValue(field.scalar)(value);
74 }
75 if (field.enum !== undefined) {
76 if (field.enum.open) {
77 // Open enums accept unrecognized values, but enum values are always
78 // int32 (see https://protobuf.dev/programming-guides/proto3/#enum).
79 return checkScalarValue(ScalarType.INT32)(value);
80 }
81 return field.enum.values.some((v) => v.number === value);
82 }
83 return isReflectMessage(value, field.message);
84 }
85 /**
86 * Return the check for values of the given scalar type.
87 *
88 * @private
89 */
90 export function checkScalarValue(scalar) {
91 switch (scalar) {
92 case ScalarType.DOUBLE:
93 return (value) => typeof value == "number";
94 case ScalarType.FLOAT:
95 return (value) => {
96 if (typeof value != "number") {
97 return false;
98 }
99 if (Number.isNaN(value) || !Number.isFinite(value)) {
100 return true;
101 }
102 if (value > FLOAT32_MAX || value < FLOAT32_MIN) {
103 return `${value.toFixed()} out of range`;
104 }
105 return true;
106 };
107 case ScalarType.INT32:
108 case ScalarType.SFIXED32:
109 case ScalarType.SINT32:
110 // signed
111 return (value) => {
112 if (typeof value !== "number" || !Number.isInteger(value)) {
113 return false;
114 }
115 if (value > INT32_MAX || value < INT32_MIN) {
116 return `${value.toFixed()} out of range`;
117 }
118 return true;
119 };
120 case ScalarType.FIXED32:
121 case ScalarType.UINT32:
122 // unsigned
123 return (value) => {
124 if (typeof value !== "number" || !Number.isInteger(value)) {
125 return false;
126 }
127 if (value > UINT32_MAX || value < 0) {
128 return `${value.toFixed()} out of range`;
129 }
130 return true;
131 };
132 case ScalarType.BOOL:
133 return (value) => typeof value == "boolean";
134 case ScalarType.STRING:
135 return (value) => {
136 if (typeof value != "string") {
137 return false;
138 }
139 return getTextEncoding().checkUtf8(value) || "invalid UTF8";
140 };
141 case ScalarType.BYTES:
142 return (value) => value instanceof Uint8Array;
143 case ScalarType.INT64:
144 case ScalarType.SFIXED64:
145 case ScalarType.SINT64:
146 // signed
147 return (value) => {
148 if (typeof value == "bigint" ||
149 typeof value == "number" ||
150 (typeof value == "string" && value.length > 0)) {
151 try {
152 protoInt64.parse(value);
153 return true;
154 }
155 catch (_) {
156 return `${value} out of range`;
157 }
158 }
159 return false;
160 };
161 case ScalarType.FIXED64:
162 case ScalarType.UINT64:
163 // unsigned
164 return (value) => {
165 if (typeof value == "bigint" ||
166 typeof value == "number" ||
167 (typeof value == "string" && value.length > 0)) {
168 try {
169 protoInt64.uParse(value);
170 return true;
171 }
172 catch (_) {
173 return `${value} out of range`;
174 }
175 }
176 return false;
177 };
178 }
179 }
180 /**
181 * Format the reason why a value is invalid for a singular field.
182 *
183 * @private
184 */
185 export function reasonSingular(field, val, details) {
186 details =
187 typeof details == "string" ? `: ${details}` : `, got ${formatVal(val)}`;
188 if (field.scalar !== undefined) {
189 return `expected ${scalarTypeDescription(field.scalar)}` + details;
190 }
191 if (field.enum !== undefined) {
192 return `expected ${field.enum.toString()}` + details;
193 }
194 return `expected ${formatReflectMessage(field.message)}` + details;
195 }
196 export function formatVal(val) {
197 switch (typeof val) {
198 case "object":
199 if (val === null) {
200 return "null";
201 }
202 if (val instanceof Uint8Array) {
203 return `Uint8Array(${val.length})`;
204 }
205 if (Array.isArray(val)) {
206 return `Array(${val.length})`;
207 }
208 if (isReflectList(val)) {
209 return formatReflectList(val.field());
210 }
211 if (isReflectMap(val)) {
212 return formatReflectMap(val.field());
213 }
214 if (isReflectMessage(val)) {
215 return formatReflectMessage(val.desc);
216 }
217 if (isMessage(val)) {
218 return `message ${val.$typeName}`;
219 }
220 return "object";
221 case "string":
222 return val.length > 30 ? "string" : `"${val.split('"').join('\\"')}"`;
223 case "boolean":
224 return String(val);
225 case "number":
226 return String(val);
227 case "bigint":
228 return String(val) + "n";
229 default:
230 // "symbol" | "undefined" | "object" | "function"
231 return typeof val;
232 }
233 }
234 function formatReflectMessage(desc) {
235 return `ReflectMessage (${desc.typeName})`;
236 }
237 function formatReflectList(field) {
238 switch (field.listKind) {
239 case "message":
240 return `ReflectList (${field.message.toString()})`;
241 case "enum":
242 return `ReflectList (${field.enum.toString()})`;
243 case "scalar":
244 return `ReflectList (${ScalarType[field.scalar]})`;
245 }
246 }
247 function formatReflectMap(field) {
248 switch (field.mapKind) {
249 case "message":
250 return `ReflectMap (${ScalarType[field.mapKey]}, ${field.message.toString()})`;
251 case "enum":
252 return `ReflectMap (${ScalarType[field.mapKey]}, ${field.enum.toString()})`;
253 case "scalar":
254 return `ReflectMap (${ScalarType[field.mapKey]}, ${ScalarType[field.scalar]})`;
255 }
256 }
257 function scalarTypeDescription(scalar) {
258 switch (scalar) {
259 case ScalarType.STRING:
260 return "string";
261 case ScalarType.BOOL:
262 return "boolean";
263 case ScalarType.INT64:
264 case ScalarType.SINT64:
265 case ScalarType.SFIXED64:
266 return "bigint (int64)";
267 case ScalarType.UINT64:
268 case ScalarType.FIXED64:
269 return "bigint (uint64)";
270 case ScalarType.BYTES:
271 return "Uint8Array";
272 case ScalarType.DOUBLE:
273 return "number (float64)";
274 case ScalarType.FLOAT:
275 return "number (float32)";
276 case ScalarType.FIXED32:
277 case ScalarType.UINT32:
278 return "number (uint32)";
279 case ScalarType.INT32:
280 case ScalarType.SFIXED32:
281 case ScalarType.SINT32:
282 return "number (int32)";
283 }
284 }