🚨 Active Supply Chain Attack:node-ipc Package Compromised.Learn More
Socket
Book a DemoSign in
Socket

json-from-default-schema

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-from-default-schema

Create json from schema and environment.

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
39
333.33%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 17 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts