/parse-ts

Validate json with types!

Primary LanguageTypeScriptISC LicenseISC

parse-ts

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

Installation

npm i -S parse-ts

Example

// 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.

Docs

Source has JSDoc with examples.

See Also

  • io-ts Similar to parse-ts but more powerful. Worth a look if you want more than simple JSON parsing.