This repository was archived by the owner on Mar 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Proposal: API for assertion messages #1
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or request
Description
As we know, runtype only tells you if an type-check passed or not, and doesn't tell why it failed. This proposal outlines the new API for validation messages.
Description
Currently, Predicate is a function that takes x and guards it against a type. This proposal recommends the addition of a validate method to Predicate which returns the following type:
{ ok: true, value: T } | { ok: false, error: ValidationError }Where ValidationError extends Error with the properties expected and actual describing the corresponding types as string to produce a nicer error; and by default .message describing a generated error.
Example:
const Message = struct({ from: string, to: string, content: string });
// elsewhere
const res = Message.validate(msg);
if (res.ok) {
// use msg
} else {
// res.error
}Downsides:
res.okdoesn't actually guardmsg, and you get no static types like you normally would with the type guard.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request