npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-schema-merger

v0.1.9

Published

create and merge json schemas from objects

Downloads

5

Readme

json-schema-merger

create json schema from pojo and merge schemas https://www.npmjs.com/package/json-schema-merger

Motivation

this was part of my smarthome development the problem description was having an "unknown" node provide a series of json strings and then through analysis of the different jsons being able to create a schema (https://json-schema.org/) that describes the dataset

example: emit1:

    {
        "test":123
    }

emit2:

    {
        "test":456
    }

then the resulting schema should describe an object that has a test property that can be either 123 or 456 or in ts:

   type schema={test:123|456}

if the value is a date or something hat frequently changes it should be collapsed from an enum/const/type union to a more generic type at latest after a few emits(i picked 10 as a default)

create Schema

create a json schema from a json object:


const myRandomObject={
    test: 123
}

const schema=createSchema(myRandomObject)
/*
{
    type: "object",
    properties: {
        test: {
            type: "number",
            enum: [
                123,
            ],
        },
    },
    additionalProperties: false,
    required: [
        "test",
    ],
}
*/

by default all values are treated as constant values

this can be changed by passing a second argument to createSchema

createSchema(myRandomObject,{disableAssumeConst:true})

schema merging

assuming 2 schemas of similar json objects this can be used to update and refine the initial schema

const schema1={...}
const schema2={...}

// currently modifies "old" schema
schema1 = mergeSchema({
    old: schema1,
    new: schema2,
    params: { mergeLength: 10 }
})

if the amount of different values for any given property exceed mergeLength it will be merged from an enum to a generic

example:

{
    type:"string",
    enum:["1","2","3","...10"]
}

will be merged to just

{
    type:"string"
    merged:true //indicator for future attempts
}

params

params support individual mergeLength per field in the form of a dot separated path string

{
    a:{
        b:123
    }
}

=>

{
    mergeLength: 10 
    "a.b":5
}

if enumKeyList is passed it will be filled with all found object paths (and can be used to generate the param list)

Schema Validation

let say you have a schema defining what is allowed as an input and a schema defining what is provided to that input

then u can use

validateJsonSchemas({
    assigning:providingSchema
    target:inputSchema
})

then if the providing schema cannot be assigned (isnt a subset) of the inputSchema it will throw an error

TODO:

  • add more types to the schema and merging