When working with data, we want to make sure it conforms to a specific shape. To do this, we decode the data using a schema.
“Decoding” is the process of taking raw, untrusted input—which might be a simple string or a plain JSON object—and transforming it into a structured, typed value that our application can use. For example, decoding doesn’t just check if a string looks like a date; it actually converts that string into a JavaScript Date object for you.
Effect Schema provides six different APIs to decode data:
decodeUnknownSyncdecodeUnknownExitdecodeUnknownOptiondecodeUnknownEffectdecodeUnknownPromisedecodeSync
All six are curried functions. A curried function is a function that doesn’t take all its arguments at once. Instead, it takes the first argument and returns a new function that takes the next. In this case, you first pass the schema, and you get back a function that accepts the input:
const decode = Schema.decodeUnknownSync(schema); // pass the schemaconst result = decode(input); // pass the inputIn the upcoming sub-chapters, I will discuss how and when to use each of these APIs.
In the sub-chapters that follow, you will come across terms such as
SchemaError,
SchemaIssue, and issue
tree.
Don’t worry if they’re unfamiliar at first. I have a dedicated chapter,
Understanding Decode
Errors,
where I explain what these terms mean in depth.