Skip to content

Commit 12891df

Browse files
committed
Added storybook and an example component for James
1 parent ad63ab1 commit 12891df

4 files changed

Lines changed: 200 additions & 22 deletions

File tree

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,53 @@
1010
## Features
1111

1212
- Written in typescript
13-
- Infers JSON Schema
13+
- Inspired by [jtd-infer](https://jsontypedef.com/docs/jtd-infer/)
14+
- Generate JSON schema documents from example data
1415
- Supports most string formats through [json-infer-types](https://github.com/jsonhero-io/json-infer-types)
1516
- Date and times
1617
- URIs
1718
- Email Addresses
1819
- Hostnames
1920
- IP Addresses
2021
- uuids
21-
- Infers JSON Type definitions
22-
- Supports hints for discriminators (tagged unions), value-only schemas, and enums
23-
- Provide with multiple JSON documents to improve inference
22+
- Available as a CLI and a library
23+
- Supports snapshotting and restoring inference sessions
2424

2525
## Usage
2626

27+
```ts
28+
import { inferSchema } from "@jsonhero/schema-infer";
29+
30+
inferSchema({
31+
id: "abeb8b52-e960-44dc-9e09-57bb00d6b441",
32+
name: "Eric",
33+
emailAddress: "eric@example.com",
34+
website: "https://github.com/ericallam",
35+
joined: "2022-01-01",
36+
})toJSONSchema();
37+
```
38+
39+
Infers the following JSON schema:
40+
41+
```json
42+
{
43+
"$schema": "https://json-schema.org/draft/2020-12/schema",
44+
"type": "object",
45+
"properties": {
46+
"id": { "type": "string", "format": "uuid" },
47+
"name": { "type": "string" },
48+
"emailAddress": { "type": "string", "format": "email" },
49+
"website": { "type": "string", "format": "uri" },
50+
"joined": { "type": "string", "format": "date" }
51+
},
52+
"required": ["id", "name", "emailAddress", "website", "joined"]
53+
}
54+
```
55+
56+
## Examples
57+
2758
## Roadmap
2859

60+
- Add support for hints for discriminators (tagged unions), value-only schemas, and enums
61+
- Add support for [JSON Typedefs](https://jsontypedef.com)
2962
- Add "verbose" mode to include `$id`, `examples`, etc.

package-lock.json

Lines changed: 142 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
"@types/node": "^16.11.7",
4343
"@typescript-eslint/eslint-plugin": "^5.8.1",
4444
"@typescript-eslint/parser": "^5.8.1",
45+
"ajv": "^8.8.2",
46+
"ajv-formats": "^2.1.1",
4547
"eslint": "^8.5.0",
4648
"eslint-config-prettier": "^8.3.0",
4749
"eslint-plugin-prettier": "^4.0.0",
@@ -75,4 +77,4 @@
7577
"bin": {
7678
"schema-infer": "./cli/schema-infer.js"
7779
}
78-
}
80+
}

tests/jsonSchema.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { readFileSync } from "fs";
33
import { inferSchema, restoreSnapshot, SchemaInferrer } from "../src";
44

55
const toSchema = (s: SchemaInferrer): Schema => s.toJSONSchema({ includeSchema: false });
6+
const readFixture = (s: string): unknown =>
7+
JSON.parse(readFileSync(`./tests/json/${s}.json`, "utf8").toString());
68
const fixtureToSchema = (s: string): Schema =>
7-
inferSchema(JSON.parse(readFileSync(`./tests/json/${s}.json`, "utf8").toString())).toJSONSchema({
9+
inferSchema(readFixture(s)).toJSONSchema({
810
includeSchema: false,
911
});
1012

@@ -352,3 +354,18 @@ describe("real world tests", () => {
352354
expect(fixtureToSchema("tweets")).toMatchSnapshot();
353355
});
354356
});
357+
358+
import Ajv2020 from "ajv/dist/2020";
359+
import addFormats from "ajv-formats";
360+
const ajv = new Ajv2020();
361+
addFormats(ajv);
362+
363+
describe("validation", () => {
364+
it("should pass validation if given the same json the schema was inferred from", () => {
365+
const schema = fixtureToSchema("tweets");
366+
const validate = ajv.compile(schema);
367+
const tweetsJson = readFixture("tweets");
368+
369+
expect(validate(tweetsJson)).toBe(true);
370+
});
371+
});

0 commit comments

Comments
 (0)