1
0
Fork
You've already forked jsonaccess
0
Simple type-safe access to JSON for Typescript
TypeScript 58.3%
Shell 41.7%
Harald Kirsch 826edc27ba Adds consumer parameter to allValid to receive the first error found
This will not be called if allValid returns true, otherwise the error found to
make it false will be provided to the consumer.
2025年07月03日 14:50:11 +02:00
bashbuilder Adds Maybe.getInvalid() to get all JsonAccessErrors from an object or array 2025年07月03日 12:10:31 +02:00
src Adds consumer parameter to allValid to receive the first error found 2025年07月03日 14:50:11 +02:00
.eslintrc.json Extends functionality significantly with jsonAsType, jsonAsTypedArray, jsonAsInstance 2025年04月08日 16:36:01 +02:00
.gitignore Improves scripts and setup for publishing the package to codeberg 2023年12月08日 11:08:40 +01:00
.npmrc Improves scripts and setup for publishing the package to codeberg 2023年12月08日 11:08:40 +01:00
.prettierrc.json Initial commit 2023年12月04日 16:57:45 +01:00
bb Fixes publishing docs 2025年07月03日 12:39:29 +02:00
dependencies.svg Fixes dependencies.svg dependency 2025年04月21日 13:55:37 +02:00
jasmine.json Changes build process to use bashbuilder 2025年04月12日 20:16:02 +02:00
package.json Adds consumer parameter to allValid to receive the first error found 2025年07月03日 14:50:11 +02:00
README.md Fixes npm publish and adds installation instruction to README.md 2025年04月13日 09:07:08 +02:00
tsconfig.json Changes build process to use bashbuilder 2025年04月12日 20:16:02 +02:00

JsonAccess — simple type-safe access to JSON for Typescript

Receiving some string text from an API which is supposedly JSON with a given structure, we want to parse the JSON and if this does not fail, access the resulting structured data in a typesafe manner.

Consider an API which is expected to deliver a tuple with type:

[string, number]

and we want to savely access the values to create an Item:

 const data = jsonParse(text);
 type Item = { id: string; price: number };
 const item: Maybe<Item> = jsonAsType(
 data,
 [jsonStringGetter('item'), jsonNumberGetter('price')],
 (i, p) => ({ item: i, price: p })
 );
 if (!isValid(item)) {
 throw item; // a JsonAccessError
 }
 // continue with item properly typed as Item

For more information see the api documentation.

Setup

To install the package, follow the instruction on codeberg's package page.

Alternatives

TypeScript-first schema validation with static type inference: zod