json-from-default-schema
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Json-from-default-schema

Create json from a schema.

Example

const json_from_schema = require('json-from-default-schema');

const config = `
{
    "message": "hello world"
}
`
const schema = `
{
    "type": "object",
    "properties": {
        "number": {
            "type": "number",
            "env": "NUMBER",
            "default": 4.3
        },
        "integer": {
            "type": "integer",
            "default": 7
        },
        "empty": {
            "type": "integer",
            "env": "EMPTY"
        },
        "object": {
            "type": "object",
            "default": {},
            "properties": {
                "array": {
                    "type": "array",
                    "env": "OBJECT_ARRAY",
                    "default": []
                },
                "number": {
                    "type": "number",
                    "default": 14.3
                },
                "integer": {
                    "type": "integer",
                    "default": 17
                },
                "empty": {
                    "type": "integer",
                    "env": "OBJECT_EMPTY"
                },
                "object": {
                    "type": "object",
                    "default": {},
                    "properties": {
                        "number": {
                            "type": "number",
                            "default": 14.3
                        },
                        "integer": {
                            "type": "integer",
                            "default": 17
                        },
                        "empty": {
                            "type": "integer",
                            "env": "OBJECT_OBJECT_EMPTY"
                        }
                    }
                }
            }
        }
    }
}
`

process.env["NUMBER"] = "455"
process.env["EMPTY"] = "88"
process.env["OBJECT_EMPTY"] = "881"
process.env["OBJECT_ARRAY"] = "[\"880\"]"
process.env["OBJECT_OBJECT_EMPTY"] = "851"

const result_config = json_from_schema(config, schema);

console.log(result_config);
/*
{
    message: 'hello world',
    number: 455,
    integer: 7,
    empty: 88,
    object: {
        array: [ '880' ],
        number: 14.3,
        integer: 17,
        empty: 881,
        object: { 
            number: 14.3, 
            integer: 17, 
            empty: 851 
        }
    }
}
*/

fn(entity: any, schema: any, env_flag: boolean = true) → Object

Create json from a schema.

params:

  • entity - source json object
  • schema - json schema
  • env_flag - Flag for parsing environment keys from "env" key

Readme

Keywords

none

Package Sidebar

Install

npm i json-from-default-schema

Weekly Downloads

1

Version

1.0.1

License

ISC

Unpacked Size

6.21 kB

Total Files

4

Last publish

Collaborators

  • samuray