Skip to content

Commit ad63ab1

Browse files
committed
Completed first stage of schema inference
1 parent 652c296 commit ad63ab1

15 files changed

Lines changed: 2692 additions & 243 deletions

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
lib
2+
lib
3+
cli

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323
- Provide with multiple JSON documents to improve inference
2424

2525
## Usage
26+
27+
## Roadmap
28+
29+
- Add "verbose" mode to include `$id`, `examples`, etc.

cli/schema-infer.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env/node
2+
3+
const { readFileSync } = require("fs");
4+
const yargs = require("yargs");
5+
const { inferSchema } = require("../lib");
6+
7+
const builder = (command) =>
8+
command.positional("file", {
9+
describe: "The file to infer the schema from",
10+
type: "string",
11+
});
12+
13+
const handler = ({ file }) => {
14+
// Read the file and parse the json
15+
const raw = readFileSync(file, "utf8").toString();
16+
17+
const document = JSON.parse(raw);
18+
19+
const inferredSchema = inferSchema(document);
20+
21+
const schema = inferredSchema.toJSONSchema({ includeSchema: true });
22+
23+
console.log(JSON.stringify(schema, null, 2));
24+
};
25+
26+
yargs.command("$0 <file>", "Infer the schema from a json file", builder, handler).parse();

0 commit comments

Comments
 (0)