Closed utrumo closed 3 years ago
JsonFromString
goes from string
to Json
, so you need a decoder which goes from unknown
to string
and then you can compose them with pipe
:
const ExternalDataValidator = t.type({
id: t.number,
date: DateFromISOString,
nestedJson: t.string.pipe(JsonFromString)
})
@gcanti, thank you! It's works. How can i add type defenition to json content?
Go on with your pipeline
const ExternalDataValidator = t.type({
id: t.number,
date: DateFromISOString,
nestedJson: t.string.pipe(JsonFromString).pipe(
t.type({
message: t.string
})
)
})
@gcanti, thank you. You solved my problem. :)
Can anybody help me to understand how to use JsonFromString inside t.type, how to specify json string and expected json content, how to make functions decode and encode working for it?
my code
```TypeScript // yarn add -D io-ts fp-ts io-ts-reporters io-ts-types monocle-ts newtype-ts import { isLeft } from 'fp-ts/lib/Either'; import * as t from 'io-ts'; import reporter from 'io-ts-reporters'; import { DateFromISOString, JsonFromString } from 'io-ts-types'; const ExternalDataValidator = t.type({ id: t.number, date: DateFromISOString, // nestedJson: JsonFromString // <--- this line }); type IExternalDataValidator = t.TypeOfThere is a link to codesandbox playground with demonstartion of problem: https://codesandbox.io/s/rs914?file=/src/index.ts
When i uncomment 9 line - i have gеt an error:
![JsonFromStringDecoder](https://user-images.githubusercontent.com/22612354/109388142-53f8fc80-7916-11eb-8ff5-1c164b29d118.png)