<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michał Podwórny</title>
    <description>The latest articles on DEV Community by Michał Podwórny (@mskv).</description>
    <link>https://dev.to/mskv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F311344%2Fcb572177-86d3-4633-9e11-808cf143a0fe.png</url>
      <title>DEV Community: Michał Podwórny</title>
      <link>https://dev.to/mskv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mskv"/>
    <language>en</language>
    <item>
      <title>TypeScript tutorial by example: implementing a well-typed validation library</title>
      <dc:creator>Michał Podwórny</dc:creator>
      <pubDate>Wed, 08 Jan 2020 00:57:00 +0000</pubDate>
      <link>https://dev.to/mskv/typescript-tutorial-by-example-implementing-a-well-typed-validation-library-3j0n</link>
      <guid>https://dev.to/mskv/typescript-tutorial-by-example-implementing-a-well-typed-validation-library-3j0n</guid>
      <description>&lt;p&gt;Throughout this article, we will be examining and explaining some of &lt;a href="https://github.com/mskv/valid-ts/"&gt;ValidTs&lt;/a&gt;'s code. Even an experienced TypeScript user may learn a trick or two. The reader is expected to have a general understanding of the language.&lt;/p&gt;

&lt;h1&gt;
  
  
  TLDR
&lt;/h1&gt;

&lt;p&gt;Here is a list of links to some interesting TypeScript features that we will be using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type"&gt;&lt;code&gt;unknown&lt;/code&gt; type&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions"&gt;tagged union&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#conditional-types"&gt;conditional types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards"&gt;type guards&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions"&gt;assertion functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions"&gt;&lt;code&gt;const&lt;/code&gt; assertions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#generic-rest-parameters"&gt;tuple type inference from generic rest parameters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/functions.html#overloads"&gt;function overloads&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#index-types-and-index-signatures"&gt;index types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types"&gt;mapped types&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Validation in general
&lt;/h1&gt;

&lt;p&gt;When we deal with any external data source, we can make no assumptions about the data received. It is quite common to see a web server's private API just cast the result of &lt;code&gt;JSON.parse&lt;/code&gt; to some known type, or even leave it as &lt;code&gt;any&lt;/code&gt;. An example explanation for doing that may go as follows: "This is a private API anyway and the same team works on the client-side code". It is quite convenient when you are just hacking around, but not very scalable. Best case scenario, invalid client requests end up as "cannot read X of undefined" in server-side error reporting. Worst case, something unexpected happens.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JSON.parse&lt;/code&gt; has always returned &lt;code&gt;any&lt;/code&gt;. However, I would say that with &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type"&gt;the introduction of &lt;code&gt;unknown&lt;/code&gt;&lt;/a&gt; type to TypeScript it seems that &lt;code&gt;unknown&lt;/code&gt; would be a better-fitting return type for it. &lt;code&gt;any&lt;/code&gt; encourages people to use something in any way, while &lt;code&gt;unknown&lt;/code&gt; requires some work. If you want to see an example of how a statically typed language handles JSON parsing, take a gander at &lt;a href="https://guide.elm-lang.org/effects/json.html"&gt;Elm's JSON Decoders&lt;/a&gt;. The idea behind the &lt;a href="https://github.com/mskv/valid-ts/"&gt;ValidTs&lt;/a&gt; library is to allow the user to easily define validators that safely turn &lt;code&gt;any&lt;/code&gt; into concrete types.&lt;/p&gt;

&lt;h1&gt;
  
  
  Result type
&lt;/h1&gt;

&lt;p&gt;All validators return a &lt;a href="https://github.com/mskv/valid-ts/blob/fb2028a/src/result.ts"&gt;result&lt;/a&gt;. It is either a success or an error. We use a &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions"&gt;tagged union&lt;/a&gt; to define it, as it is very easy for TypeScript to infer properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum ResultKind { Ok, Err }

type Ok&amp;lt;T&amp;gt; = { kind: ResultKind.Ok; value: T };
type AnyOk = Ok&amp;lt;any&amp;gt;;
type Err&amp;lt;T&amp;gt; = { kind: ResultKind.Err; value: T };
type AnyErr = Err&amp;lt;any&amp;gt;;

type Result&amp;lt;O, E&amp;gt; = Ok&amp;lt;O&amp;gt; | Err&amp;lt;E&amp;gt;;
type AnyResult = AnyOk | AnyErr;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that an &lt;code&gt;enum&lt;/code&gt; defined like this will use integers in place of &lt;code&gt;Ok&lt;/code&gt; and &lt;code&gt;Err&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With the introduction of &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#conditional-types"&gt;conditional types&lt;/a&gt;, it is easy to turn &lt;code&gt;Ok&amp;lt;number&amp;gt; | Err&amp;lt;"invalid_number"&amp;gt;&lt;/code&gt; into &lt;code&gt;Ok&amp;lt;number&amp;gt;&lt;/code&gt; with &lt;code&gt;FilterOk&lt;/code&gt; or &lt;code&gt;Err&amp;lt;"invalid_number"&amp;gt;&lt;/code&gt; with &lt;code&gt;FilterErr&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type FilterOk&amp;lt;T extends AnyResult&amp;gt; = Extract&amp;lt;T, { kind: ResultKind.Ok }&amp;gt;;
type FilterErr&amp;lt;T extends AnyResult&amp;gt; = Extract&amp;lt;T, { kind: ResultKind.Err }&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We also define another helper that just turns &lt;code&gt;Ok&amp;lt;number&amp;gt;&lt;/code&gt; into &lt;code&gt;number&lt;/code&gt; or &lt;code&gt;Err&amp;lt;"invalid_number"&amp;gt;&lt;/code&gt; into &lt;code&gt;"invalid_number"&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type UnwrapOk&amp;lt;O extends AnyOk&amp;gt; = O["value"];
type UnwrapErr&amp;lt;E extends AnyErr&amp;gt; = E["value"];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Instead of a comparison &lt;code&gt;result.kind === ResultKind.Ok&lt;/code&gt; we might want to use a helper function. Here is the definition of our &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards"&gt;type guard&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const isOk = &amp;lt;R extends AnyResult&amp;gt;(result: R): 
  result is FilterOk&amp;lt;R&amp;gt; =&amp;gt; result.kind === ResultKind.Ok;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions"&gt;TypeScript 3.7&lt;/a&gt; we can also define analogous assertions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function assertOk&amp;lt;R extends AnyResult&amp;gt;(result: R): 
  asserts result is FilterOk&amp;lt;R&amp;gt; {
    if (!isOk(result)) { throw new Error("Expected Ok"); }
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Armed with those helpers we can progress to our validators.&lt;/p&gt;

&lt;h1&gt;
  
  
  Validator type
&lt;/h1&gt;

&lt;p&gt;We define our &lt;a href="https://github.com/mskv/valid-ts/blob/711c97f/src/validators/validator.ts"&gt;validator&lt;/a&gt; as a function that accepts any value and returns some result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Validator&amp;lt;I, O extends AnyResult&amp;gt; = (input: I) =&amp;gt; O;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The idea about returning a &lt;code&gt;Result&lt;/code&gt; instead of a &lt;code&gt;boolean&lt;/code&gt; to indicate the result of the validation is that we want to allow our validators to change their input and return the result of that change as their successful output. This will make them more flexible by allowing casting/coercion of input to happen inside them.&lt;/p&gt;

&lt;p&gt;Again, using &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#conditional-types"&gt;conditional types&lt;/a&gt;, we are able to get the input and output types of our validators whenever we need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ExtractValidatorI&amp;lt;V&amp;gt; = 
  V extends Validator&amp;lt;infer I, any&amp;gt; ? I : never;
type ExtractValidatorO&amp;lt;V&amp;gt; = 
  V extends Validator&amp;lt;any, infer O&amp;gt; ? O : never;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Simple validators
&lt;/h1&gt;

&lt;p&gt;Let us start by implementing a simple &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e16/src/validators/eq.ts"&gt;equality validator&lt;/a&gt;. To implement any validator, all we need to do is to satisfy the &lt;code&gt;Validator&amp;lt;I, O&amp;gt;&lt;/code&gt; interface listed above. The equality validator accepts any input. If the input matches the expected value, it returns &lt;code&gt;Ok&amp;lt;T&amp;gt;&lt;/code&gt;. Otherwise, it will report &lt;code&gt;Err&amp;lt;"equality_error"&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type EqOutput&amp;lt;T&amp;gt; = Ok&amp;lt;T&amp;gt; | Err&amp;lt;"equality_error"&amp;gt;;

const eq = &amp;lt;T&amp;gt;(expectedValue: T): Validator&amp;lt;any, EqOutput&amp;lt;T&amp;gt;&amp;gt; =&amp;gt;
  (input) =&amp;gt; input === expectedValue 
    ? ok(input) 
    : err("equality_error");
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That is it. Now any value that succeeds the equality check will be correctly typed. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const validator = eq("some_const_string" as const)
const validation = validator(&amp;lt;input&amp;gt;)

if (isOk(validation)) {
  // validation.value is correctly typed to "some_const_string"
} else {
  // validation.value is correctly typed to "equality_error"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note the use of &lt;code&gt;as const&lt;/code&gt; available from &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions"&gt;Typescript 3.4&lt;/a&gt; onwards. Thanks to it, the expression &lt;code&gt;"some_const_string"&lt;/code&gt; is typed as &lt;code&gt;"some_const_string"&lt;/code&gt; instead of just &lt;code&gt;string&lt;/code&gt;. It is a very useful tool for any constant value, not just strings.&lt;/p&gt;

&lt;p&gt;Take a quick look at &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e165c759ffcf2b7078c637577adf14c80685/src/validators/incl.ts"&gt;&lt;code&gt;incl&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e165c759ffcf2b7078c637577adf14c80685/src/validators/primitives.ts"&gt;&lt;code&gt;number&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e165c759ffcf2b7078c637577adf14c80685/src/validators/optional.ts"&gt;&lt;code&gt;optional&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e165c759ffcf2b7078c637577adf14c80685/src/validators/nullable.ts"&gt;&lt;code&gt;nullable&lt;/code&gt;&lt;/a&gt; to see other simple validator examples.&lt;/p&gt;

&lt;h1&gt;
  
  
  Complex validators
&lt;/h1&gt;

&lt;h3&gt;
  
  
  "Or" validator
&lt;/h3&gt;

&lt;p&gt;Let us try to tackle the &lt;a href="https://github.com/mskv/valid-ts/blob/41264c2/src/validators/or.ts"&gt;&lt;code&gt;or&lt;/code&gt; validator&lt;/a&gt; first. Here is the usage example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const validator = or(string, number, boolean)
const validation = validator(&amp;lt;input&amp;gt;)

if (isOk(validation)) {
  // validation.value is correctly typed to `string | number | boolean`
} else {
  // validation.value is correctly typed to
  // {
  //   kind: "all_failed",
  //   errors: Array&amp;lt;
  //     "string_error" | "number_error" | "boolean_error"
  //   &amp;gt;
  // }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we can see, &lt;code&gt;or&lt;/code&gt; validator constructor is a variadic function - it has infinite arity. Its return type is a &lt;code&gt;Validator&amp;lt;OrInput, OrOutput&amp;gt;&lt;/code&gt;. To type &lt;code&gt;OrInput&lt;/code&gt; and &lt;code&gt;OrOutput&lt;/code&gt;, we need to look at the validators passed to the constructor. &lt;/p&gt;

&lt;p&gt;Here is a trick: to turn the tuple &lt;code&gt;[boolean, string]&lt;/code&gt; into a union type &lt;code&gt;boolean | string&lt;/code&gt; (or an array &lt;code&gt;Array&amp;lt;boolean | string&amp;gt;&lt;/code&gt; into &lt;code&gt;boolean | string&lt;/code&gt;), you can pick &lt;code&gt;[number]&lt;/code&gt; from it: &lt;code&gt;[boolean, string][number]&lt;/code&gt;. We will use this to get combined &lt;code&gt;Ok&lt;/code&gt; and &lt;code&gt;Err&lt;/code&gt; types from all the different validators passed to &lt;code&gt;or&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let us now define the &lt;code&gt;or&lt;/code&gt; validator constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const or = &amp;lt;Vs extends AnyValidator[]&amp;gt;(...validators: Vs):
  Validator&amp;lt;OrInput&amp;lt;Vs&amp;gt;, OrOutput&amp;lt;Vs&amp;gt;&amp;gt; =&amp;gt; {
    // (...)
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As promised, it is a variadic function that returns a validator. Using the trick mentioned above and our &lt;code&gt;ExtractValidatorI&lt;/code&gt; helper, we can define the input of the combined validator as an alternative of all the validator inputs passed to the constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type OrInput&amp;lt;Vs extends AnyValidator[]&amp;gt; = 
  ExtractValidatorI&amp;lt;Vs[number]&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Typing the output is a bit more complicated. We want an alternative of all the successes or all the errors wrapped in "all failed" error. We can take advantage of all the helpers defined above: &lt;code&gt;ExtractValidatorO&lt;/code&gt;, &lt;code&gt;FilterOk&lt;/code&gt;, &lt;code&gt;FilterErr&lt;/code&gt; and &lt;code&gt;UnwrapErr&lt;/code&gt;. Take a look at the final result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type OrOutput&amp;lt;Vs extends AnyValidator[]&amp;gt; = 
  OrOutputOk&amp;lt;Vs&amp;gt; | OrOutputErr&amp;lt;Vs&amp;gt;;
type OrOutputOk&amp;lt;Vs extends AnyValidator[]&amp;gt; = 
  FilterOk&amp;lt;ExtractValidatorO&amp;lt;Vs[number]&amp;gt;&amp;gt;;
type OrOutputErr&amp;lt;Vs extends AnyValidator[]&amp;gt; =
  Err&amp;lt;
    {
      kind: "all_failed",
      errors: Array&amp;lt;
        UnwrapErr&amp;lt;FilterErr&amp;lt;ExtractValidatorO&amp;lt;Vs[number]&amp;gt;&amp;gt;&amp;gt;
      &amp;gt;,
    }
  &amp;gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That is it! We have just defined a function that accepts an infinite number of arguments and correctly infers the input, success and error types of the generated validator based on those arguments. Note how nicely it composes with all the other validator functions we have. Also note that there is nothing stopping us from passing any custom validator to &lt;code&gt;or&lt;/code&gt;, even an anonymous function.&lt;/p&gt;

&lt;h3&gt;
  
  
  "And" validator
&lt;/h3&gt;

&lt;p&gt;Our &lt;a href="https://github.com/mskv/valid-ts/blob/7ddc22d/src/validators/and.ts"&gt;&lt;code&gt;and&lt;/code&gt; validator&lt;/a&gt; works similarly to the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator. It constructs a validator that reports the first error encountered. If no error occurs, the output of the last validator is returned. Each validator feeds its output as input to the next one. I am not too well-versed in functional programming, but I would say that it works not unlike &lt;a href="https://hackage.haskell.org/package/base-4.6.0.1/docs/Control-Monad.html#v:-62--61--62-"&gt;Kleisli composition of the Either monad&lt;/a&gt;. Here is the usage example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const validator = and(string, (str) =&amp;gt; {
  // Note that `str` is typed as `string`
  const parsed = parseInt(str)

  return Number.isNan(parsed) 
    ? err("cast_integer_error" as const) 
    : ok(parsed)
})
const validation = validator("123")

if (isOk(validation)) {
  // validation.value is typed as `number` 
  // and has value of `123`
} else {
  // validation.value is typed as 
  // `"string_error" | "cast_integer_error"`
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is quite complicated to express the "each validator feeds its outputs as input to the next one" part. For instance, we want the hypothetical &lt;code&gt;and(string, moreThan(3))&lt;/code&gt; to fail at compile time, assuming that &lt;code&gt;string&lt;/code&gt; validator outputs a value of type &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;moreThan(3)&lt;/code&gt; expects an input of type &lt;code&gt;number&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I have found no other way to achieve this other than by extensive use of &lt;a href="https://www.typescriptlang.org/docs/handbook/functions.html#overloads"&gt;function overloads&lt;/a&gt; and defining each possible case separately for every arity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface And {
  // (...)
  // case for arity 4
  // case for arity 3
  // case for arity 2
  // case for infinite arity
}

export const and: And = (...validators: any) =&amp;gt; {
  // (...)
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here is what I have done for arity of two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;
  I1, 
  O1 extends AnyResult, 
  I2 extends UnwrapOk&amp;lt;FilterOk&amp;lt;O1&amp;gt;&amp;gt;, 
  O2 extends AnyResult
&amp;gt;(v1: Validator&amp;lt;I1, O1&amp;gt;, v2: Validator&amp;lt;I2, O2&amp;gt;): 
  Validator&amp;lt;I1, O2 | FilterErr&amp;lt;O1&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The important parts to see are &lt;code&gt;I2 extends UnwrapOk&amp;lt;FilterOk&amp;lt;O1&amp;gt;&amp;gt;&lt;/code&gt; (which ensures that the second validator expects to receive the successful output of the previous validator as its input) and &lt;code&gt;Validator&amp;lt;I1, O2 | FilterErr&amp;lt;O1&amp;gt;&amp;gt;&lt;/code&gt; (which tells us what the resulting validator expects and returns).&lt;/p&gt;

&lt;p&gt;We cannot define a case for every arity. I have defined a compromise catch-all case to handle infinite arity at the expense of validating the part that "the next validator expects to receive the successful output of the previous validator as its input".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Vs extends AnyValidator[]&amp;gt;(...validators: Vs): Validator&amp;lt;
  ExtractValidatorI&amp;lt;Vs[0]&amp;gt;, 
  FilterOk&amp;lt;ExtractValidatorO&amp;lt;LastTupleElem&amp;lt;Vs&amp;gt;&amp;gt;&amp;gt; | 
    FilterErr&amp;lt;ExtractValidatorO&amp;lt;Vs[number]&amp;gt;&amp;gt;
&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, we have replaced &lt;code&gt;I1&lt;/code&gt; from the previous example with &lt;code&gt;ExtractValidatorI&amp;lt;Vs[0]&amp;gt;&lt;/code&gt;. Since &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#generic-rest-parameters"&gt;TypeScript 3.0&lt;/a&gt; generic variadic arguments are treated as tuples. In the example above, the generic &lt;code&gt;Vs&lt;/code&gt; type gets inferred as a tuple and we can pick the first element from it: &lt;code&gt;Vs[0]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;O2 |&lt;/code&gt; has been replaced with &lt;code&gt;FilterOk&amp;lt;ExtractValidatorO&amp;lt;LastTupleElem&amp;lt;Vs&amp;gt;&amp;gt;&amp;gt; |&lt;/code&gt;. It takes the last element of the &lt;code&gt;Vs&lt;/code&gt; tuple, extracts the output of that validator and filters its success. &lt;code&gt;LastTupleElem&lt;/code&gt; is quite interesting here. To implement that, I have stolen a &lt;a href="https://github.com/andnp/SimplyTyped/blob/eb2a6f973005f5bde73f096499938f7ad286c3f8/src/types/numbers.ts#L23"&gt;trick from SimpleTyped library&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Prev&amp;lt;T extends number&amp;gt; = [-1, 0, 1, 2, 3, 4, 5, 6, 7, (...)
type Length&amp;lt;T extends any[]&amp;gt; = T["length"];
type LastTupleElem&amp;lt;T extends any[]&amp;gt; = T[Prev&amp;lt;Length&amp;lt;T&amp;gt;&amp;gt;];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There we go! We have got a very powerful tool to express a set of validations that can also include casting and coercing. We can define a whole pipeline to be performed on a particular value.&lt;/p&gt;

&lt;h3&gt;
  
  
  "Shape" validator
&lt;/h3&gt;

&lt;p&gt;The last validator we will examine is the &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e16/src/validators/shape.ts"&gt;&lt;code&gt;shape&lt;/code&gt; validator&lt;/a&gt;. It allows defining a validator based on the given object shape. As always, the type of successful and erroneous validation all get correctly inferred. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const validator = shape({
  name: string,
  age: and(string, (str) =&amp;gt; {
    const parsed = parseInt(str)

    return Number.isNan(parsed) 
      ? err("cast_integer_error" as const) 
      : ok(parsed)
  })
})
const validation = validator(&amp;lt;anything&amp;gt;)

if (isOk(validation)) {
  // validation.value is typed as `{ name: string, age: number}`
} else {
  // validation.value is typed as
  // {
  //   kind: "shape_error",
  //   errors: Array&amp;lt;
  //     { field: "name", error: "string_error" },
  //     { field: "age", error: "string_error" | 
  //       "cast_integer_error" },
  //   &amp;gt;
  // }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As seen from the usage, all revolves around the schema definition. We will soon find out what its type is. However, let us first define the &lt;code&gt;shape&lt;/code&gt; validator constructor as a function that accepts a schema and returns a validator with its output inferred from the schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const shape = &amp;lt;S extends Schema&amp;gt;(schema: S): 
  Validator&amp;lt;any, ShapeOutput&amp;lt;S&amp;gt;&amp;gt; =&amp;gt; (input) =&amp;gt; { (...) }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we see above, a &lt;code&gt;Schema&lt;/code&gt; is just a mapping from field to field's validator. We can achieve that with an &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#index-types-and-index-signatures"&gt;index type&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Schema = { [field: string]: AnyValidator };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ShapeOutput&lt;/code&gt; is defined as a union of &lt;code&gt;ShapeOutputOk&lt;/code&gt; and &lt;code&gt;ShapeOutputErr&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ShapeOutput&amp;lt;S extends Schema&amp;gt; = 
  ShapeOutputOk&amp;lt;S&amp;gt; | ShapeOutputErr&amp;lt;S&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The definition of &lt;code&gt;ShapeOutputOk&lt;/code&gt; takes advantage of the helper functions we already know and &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types"&gt;mapped types&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ShapeOutputOk&amp;lt;S extends Schema&amp;gt; = Ok&amp;lt;
  { [K in keyof S]: UnwrapOk&amp;lt;FilterOk&amp;lt;ExtractValidatorO&amp;lt;S[K]&amp;gt;&amp;gt;&amp;gt; }
&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What we do with &lt;code&gt;ShapeOutputErr&lt;/code&gt; is more complicated. Let us start with the end result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ShapeOutputErr&amp;lt;S extends Schema&amp;gt; =
  Err&amp;lt;
    {
      kind: "shape_error",
      errors: Array&amp;lt;{
        [K in keyof S]: {
          field: K,
          error: UnwrapErr&amp;lt;FilterErr&amp;lt;ExtractValidatorO&amp;lt;S[K]&amp;gt;&amp;gt;&amp;gt;,
        }
      }[keyof S]&amp;gt;,
    }
  &amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What happens is the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We have a schema:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  name: Validator&amp;lt;
    any, 
    Ok&amp;lt;string&amp;gt; | Err&amp;lt;"string_error"&amp;gt;
  &amp;gt;,
  age: Validator&amp;lt;
    any, 
    Ok&amp;lt;number&amp;gt; | Err&amp;lt;"string_error"&amp;gt; | Err&amp;lt;"cast_integer_error"&amp;gt;
  &amp;gt;,
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;We turn it into:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  name: { 
    field: "name", 
    error: "string_error" 
  },
  age: { 
    field: "name", 
    error: "string_error" | "cast_integer_error" 
  },
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;by utilising:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{

    field: K,
    error: UnwrapErr&amp;lt;FilterErr&amp;lt;ExtractValidatorO&amp;lt;S[K]&amp;gt;&amp;gt;&amp;gt;,
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Then we turn it into:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ field: "name", error: "string_error" } |
  { field: "age", error: "string_error" | "cast_integer_error" }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;by picking the fields with &lt;code&gt;[keyof S]&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lastly, we wrap it in &lt;code&gt;Array&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That would be all for this complicated case. With &lt;code&gt;or&lt;/code&gt;, &lt;code&gt;eq&lt;/code&gt; and &lt;code&gt;shape&lt;/code&gt; you can do wonky stuff, for instance automatically infer a union type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const reservationCommandValidator = or(
  shape({
    kind: eq("RequestTicketReservation" as const),
    ticketId: number
  }),
  shape({
    kind: eq("RevokeTicketReservation" as const),
    reservationId: string
  }),
  shape({
    kind: eq("ArchiveTicketReservation" as const),
    reservationId: string
  })
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I can imagine a single backend endpoint handling a successfully validated reservation request with ease and confidence.&lt;/p&gt;

&lt;p&gt;Check out some other complicated validators: &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e16/src/validators/all.ts"&gt;&lt;code&gt;all&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e16/src/validators/array.ts"&gt;&lt;code&gt;array&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/mskv/valid-ts/blob/fec2e16/src/validators/dict.ts"&gt;&lt;code&gt;dict&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Postlude
&lt;/h1&gt;

&lt;p&gt;I hope that this proves useful to someone. I find myself benefit from the features described above quite often. The more you manage to change &lt;code&gt;any&lt;/code&gt; into a concrete type, or &lt;code&gt;string&lt;/code&gt; into something like &lt;code&gt;"RequestTicketReservation"&lt;/code&gt;, the more maintainable and bug-proof your codebase gets.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>validation</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
