Validate json with types!
You ever take json data from the server and want to verify it matches some interface? This library is for you!
Depends on fp-ts
npm i -S parse-ts
// example interface
interface Rec {
a: number;
b: string;
c: number[];
d: boolean;
};
// Validator spec
const rec: RecordSpec<Rec> = {
a: num,
b: str,
c: arrayOf(num),
d: bool,
};
const someData = JSON.parse(`{"a": 1, "b": "hi", "c": [1], "d": false}`);
// usage
record(rec)(someData); // => Option<Rec>
Check out unit tests for more usage examples.
Source has JSDoc with examples.
- io-ts Similar to parse-ts but more powerful. Worth a look if you want more than simple JSON parsing.