[BUG] Standard Schema accepts values that don't match their field's type
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 51/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
- Ambito
- backend-api-design
Direzione di ricerca
Start by reading createStandardSchema and the code that constructs messages and runs buf.validate rules; the issue does not name specific files or tests. Reproduce the UInt32Value case with { value: 1.5 }, then trace how field types and paths are available during validation. Done when mismatched scalar values are reported at their field paths and valid results can be serialized.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Description
The Standard Schema from createStandardSchema returns values whose types don't match their fields as valid. Its input is unknown, which is typically form data, but create() doesn't check scalar types and the validator only evaluates the buf.validate rules. So a form with a number input for a uint32 field accepts 1.5 or -1, and the mistake only shows up later when the message fails to encode.
Steps to Reproduce
import { createStandardSchema } from "@bufbuild/protovalidate";
import { toBinary } from "@bufbuild/protobuf";
import {
BoolValueSchema,
Int32ValueSchema,
StringValueSchema,
UInt32ValueSchema,
} from "@bufbuild/protobuf/wkt";
const result = createStandardSchema(UInt32ValueSchema)["~standard"].validate({ value: 1.5 });
console.log(result); // { value: { $typeName: "google.protobuf.UInt32Value", value: 1.5 } }
toBinary(UInt32ValueSchema, result.value);
// Error: cannot encode field google.protobuf.UInt32Value.value to binary: invalid uint32: 1.5
Expected Behavior
A value that doesn't fit its field's type is reported as an issue at that field's path, as a rule violation would be, so the schema's output can always be serialized.
Actual Behavior
Every one of these is returned as valid:
| Schema | Input | toBinary of the output |
|---|---|---|
UInt32Value |
{ value: 1.5 } |
throws invalid uint32: 1.5 |
UInt32Value |
{ value: -1 } |
throws invalid uint32: -1 |
UInt32Value |
{ value: 2 ** 32 } |
throws invalid uint32: 4294967296 |
Int32Value |
{ value: 2 ** 31 } |
throws invalid int32: 2147483648 |
StringValue |
{ value: 123 } |
encodes without an error |
BoolValue |
{ value: "yes" } |
encodes without an error |
Rules on the field don't catch it either: a field declared as optional uint32 number = 2 [(buf.validate.field).required = true, (buf.validate.field).uint32.gte = 1, (buf.validate.field).uint32.lte = 1000000] accepts 1.5.
Environment
- @bufbuild/protovalidate version: 1.3.0
- @bufbuild/protobuf version: 2.17.0
- @bufbuild/cel version: 0.6.1
- @bufbuild/cel-spec version: 0.6.1
- Node.js version: v26.13.0
- Protobuf Compiler & Version: buf 1.75.0
Possible Solution
Before creating the message, createStandardSchema could check each field's value against the field's type and report any mismatch with that field's path: integers that aren't whole or fall outside a 32-bit range, and values of the wrong JS type for string, bool, bytes, 64-bit (bigint, or string/number with jstype) and enum fields. As far as I can tell, the input type MessageShape<Desc> / MessageInitShape<Desc> only describes the input statically. At runtime the input is unknown, so the schema is the only thing that can catch these.
Additional Context
We use the schema to validate forms against the same rules the server enforces. For now we work around this with our own Standard Schema that range-checks integer fields before calling validator.validate.
- Lingua principale
- TypeScript
- Stelle
- 85
- Fork
- 14
- Merge medio
- 2m
- PR unite (30g)
- 1
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di bufbuild/protovalidate-es
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 52/100
bufbuild/protovalidate-es#222 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 1-2 giorni Idoneità per principianti 55/100
bufbuild/protovalidate-es#204 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
bufbuild/protovalidate-es#188 ·
I maintainer di solito rispondono entro 1 giorno
-
Feature
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
bufbuild/protovalidate-es#133 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di bufbuild/protovalidate-es
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
melgarafael/DeskcommCRM#5614 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 76/100
slackapi/slack-health-score#253 ·
I maintainer di solito rispondono entro 1 giorno
-
feature needs triage site
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
owid/owid-grapher#8019 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
simstudioai/sim#10710 ·
I maintainer di solito rispondono entro 1 giorno
-
via-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno