Less than 1KB (gzipped) subset of Superstruct specialized in validating types of data decoded from JSON.
// https://github.com/ianstormtaylor/superstruct#usage
import { is, object, number, string, array } from 'microstruct';
const Article = object({
id: number(),
title: string(),
tags: array(string()),
author: object({
id: number(),
}),
});
const data: unknown = {
id: 34,
title: 'Hello World',
tags: ['news', 'features'],
author: {
id: 1,
},
};
if (is(data, Article)) {
// `data` is guaranteed to be of type `{ id: number; title: string; tags:
// string[]; author: { id: number } }` in this block.
}
Error reporting is outside the scope of Microstruct. Friendly error messages are not necessary unless disclosed to end users.
Because Microstruct is specialized in validating types of data decoded from JSON, create
and mask
are not supported.
parse(json, struct)
returns a typed value parsed from json
if it is valid, otherwise undefined
.
const json = '{"result":true, "count":42}';
const value = parse(json, object({ result: boolean(), count: number() }));
if (value !== undefined) {
// `value` is guaranteed to be of type `{ result: boolean; count: number }` in
// this block.
}
npm install microstruct
# or
yarn add microstruct
To use Microstruct with TypeScript, typescript >=4.1.2
is required.
Microstruct is licensed under MIT License.