Comments (2)
Hey @nvie thanks for the prompt and thorough reply! Upgrading doesn't seem like it's in the cards at the moment regrettably. I ended up doing something like this
function tryDecoderGuard<D extends d.Decoder<any>>(
decoder: D,
value: unknown,
): [Error, null] | [null, d.DecoderType<D>] {
let decodedValue = null;
let error = null;
try {
decodedValue = d.guard(decoder)(value);
} catch (e: any) {
error = e;
}
return [error, decodedValue];
}
So that way, we return a tuple with a decoded and typed value OR a guard error. The caller then can refine on that tuple and execute additional logic. I'm using this code in an Express server and going down the .map
route would have been difficult to prevent multiple res.send
from happening on a given request.
from decoders.
Note upfront: This is more complicated than it had to be, which is why I simplified it a lot in 2.0. To learn how to adopt from the old to the new APIs, see this migration guide. I would recommend it, because it also gets rid of the complexity incurred by the lemons
and debrief
dependencies. 🎉
To answer your question: Before 2.0, you can use use the debrief
library to serialize it directly. This is what the guard internally uses too (it's already a dependency).
Here's the relevant code you could copy:
Lines 29 to 35 in a396623
Where blob
is your input.
Note that there are two ways to turn an Annotation
into a textual error message:
- Using
import { serialize } from 'debrief
, then callingserialize(annotation)
. This will echo back the original input, and "inline" the error message in a human-readable form. Example of a possible returned multi-line string:{ "foo": "bar", ^^^^^ Must be integer }
- Using
import { summarize } from 'debrief
, then callingsummarize(annotation)
. This will return a list-of-strings, each containing the "path" into the object, and the error reason, without echoing back. Less human-friendly, but won't echo back any sensitive data. Example of returned array of strings:['Value at key "foo": Must be integer']
Hope that helps!
from decoders.
Related Issues (20)
- Missing field in `inexact` doc HOT 1
- Some way to influence/adjust error messages to redact certain fields in objects? HOT 3
- Question: Type intersection composition HOT 2
- Decoder for object with specified key HOT 3
- Required types HOT 6
- Dispatch does not work when the field differentiating which decoder to use has a non-string value HOT 1
- Decoder with constants is throwing an exception. TypeError: (0 , _Result.Ok) is not a function HOT 5
- Typescript error with required properties HOT 9
- [Bug] Builtin composite decoders cannot take decoders with non-`unknown` input types HOT 8
- WeakGuard in 2.x HOT 3
- Migration help needed: .map to .transform HOT 3
- Export Annotation and Formatting function from package HOT 1
- Composing `lazy` and `either` can become slow. HOT 3
- Package requires nodejs 10 or newer, but this is not documented. HOT 2
- Decoding nested serialized JSON HOT 1
- Type is not inferred correctly from a dynamically created decoder with generics HOT 4
- Union type is not inferred correctly from either decoder HOT 1
- Ability to filter out incorrect items during array decoding HOT 6
- URL decoder does not accept URLs with / in the query part HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from decoders.