1.0.1 • Published 4 years ago

@slimio/json-schema-prompt v1.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

Json-schema-prompt

version Maintenance MIT dep size Known Vulnerabilities

Fill a JavaScript plainObject by asking one or many question(s) by fetching recursively possible keys and values in a JSON Schema.

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/json-schema-prompt
# or
$ yarn add @slimio/json-schema-prompt

Usage example

const { readFile } = require("fs").promises;
const { join } = require("path");

const { fillWithSchema } = require("@slimio/json-schema-prompt");

async function main() {
    const buf = await readFile(join(__dirname, "config.json"));
    const json = JSON.parse(buf.toString());
    const object = await fillWithSchema(json);

    console.log(JSON.stringify(object, null, 4));
}
main().catch(console.error);

API

fillWithSchema(schema?: object): Promise< object >

Take a JSON Schema as input and will ask the user to fill the stdin with answer for each fetched key of the schema (if the key have a default value it's possible to skip it). At the end you will get a Javascript Object that match your JSON Schema.

const schema = {
    additionalProperties: false,
    properties: {
        foo: {
            type: "string",
            description: "foo value",
            default: "bar"
        }
    }
};

// ... fill stdin in your terminal
const payload = await fillWithSchema(schema);

console.log(payload); // { foo: "your answer|bar" }

Dependencies

NameRefactoringSecurity RiskUsage
kleurMinorLowThe fastest Node.js library for formatting terminal text with ANSI colors
lodash.setMinorLowSet deep a value in a JavaScript object
qoaMinorLowMinimal interactive command-line prompts

License

MIT