gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

add flatObject util #148

Open jituanlin opened 3 years ago

jituanlin commented 3 years ago

Decode "flat object"(whose values are not reference type), unlike t.type, the return is Record<string, string>. One use case is validating JSON from excel, and using the returned error report to display table like UI.

  const validateObject = flatObject(drawFirstLine)({
    name: D.withMessage(() => 'name must be string')(D.string),
    age: D.withMessage(() => 'age must be number')(D.number),
  })

 const error = F.pipeable.pipe(
      {
        name: 'jit',
        age: '140',
      },
      validateObject
    )

 assert.deepStrictEqual(error, {
     name: undefined,
     age: 'age must be number',
})