Accept init shapes in Standard Schema validation#169
Open
Adrastopoulos wants to merge 1 commit into
Open
Conversation
The Standard Schema validator previously required a message instance created with create(). Plain objects failed with "Cannot validate message undefined" before any rule was evaluated, even for valid data. Standard Schema consumers usually hold plain values, not message instances: form libraries validate form state, and RPC frameworks validate deserialized JSON payloads. Requiring create() at every callsite defeats the interop the interface exists for. createStandardSchema now converts a plain object to a message with create() before validating; message instances are still validated directly. Its declared types are unchanged, so existing consumers, such as TanStack Form with defaultValues: create(Schema), see no type-level change. The new createStandardSchemaInit shares the same runtime but declares MessageInitShape as its input type, for consumers whose values are plain objects: form state typed from plain default values, or RPC input validation where the schema's input type dictates what callers may pass. Errors thrown by create() for malformed init values are reported as issues instead of escaping the validator. Fixes bufbuild#95.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #95.
createStandardSchema()only accepts message instances. A plain object fails before any rule is evaluated, even when the data is valid:Standard Schema consumers usually hold plain values, not message instances: form state, or deserialized RPC payloads (e.g. TanStack Start's
inputValidator).Changes:
validate()now converts plain objects to messages withcreate()before validating. Message instances are validated directly, as before. Errors thrown bycreate()become issues, since Standard Schema validators must not throw.createStandardSchema()keeps its declared types. The Standard Schema input parameter is covariant, so widening it toMessageInitShapewould break existing consumers, e.g. TanStack Form withdefaultValues: create(Schema).createStandardSchemaInit()shares the runtime but declaresMessageInitShapeas its input, for consumers where the schema's input type dictates what callers pass.MessageInitShapeis not assignable toMessageShape, so one input type cannot serve both.All pre-existing tests pass unchanged. New tests cover the coercion, instance identity, and both type surfaces.