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

js2schema

v1.0.8

Published

Convert JavaScript Object or Array to JSON Schema while **keeping the description and infer the type** from the pattern of the value AS BEST AS I CAN.

Downloads

15

Readme

Convert JavaScript Object or Array to JSON Schema while keeping the description and inferring the type from the pattern of the value AS BEST AS I CAN.

Based on generate-schema#json but more powerful as js2schema will do it's best to keep the description and infer the type from the pattern of the values and is able to build your own JSON schema using typeResolvers.

💡 Notice: Try generate-schema#json first then use this package only if generate-schema doesn't meet your requirements.

Differences or Features

  1. Add meaningful description for every key as it can make the schema genereated form UI more readable. Readability aways counts.
  2. integer type supported when all the values are integers. more at Enhacements For JSON Schema - issue#39. More precise type means stronger system.
  3. Type will be resolved to number when any of the values is float.
  4. Auto convert integer / float string to integer / number type and it can be turned off as your wish.
  5. Last but not least. The powerful feature is you can modify the type to whatever you want, so you can build you own JavaScript type DSL. Jump to the exiciting example 😀.

Many thanks to generate-schema.

Usage

const { js2schema } = require('js2schema')

Example

Added Description and smart integer type. 😘

Take the example from GenerateSchema#json:

const schema = js2schema(
  [
    {
      id: 2,
      name: 'An ice sculpture',
      price: 12.5,
      tags: ['cold', 'ice'],
      dimensions: {
        length: 7.0,
        width: 12.0,
        height: 9.5,
      },
      warehouseLocation: {
        latitude: -78.75,
        longitude: 20.4,
      },
    },
    {
      id: 3,
      name: 'A blue mouse',
      price: 25.5,
      dimensions: {
        length: 3.1,
        width: 1.0,
        height: 1.0,
      },
      warehouseLocation: {
        latitude: 54.4,
        longitude: -32.7,
      },
    },
  ],
  { title: 'Product' },
);

Outputs:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Product Set",
  "description": "Product Set",
  "type": "array",
  "items": {
    "description": "Product",
    "title": "Product",
    "type": "object",
    "properties": {
      "id": {
        "type": "integer", // id is expected to be "integer" but "number" in the example of generate-schema
        "description": "2",
      },
      "name": {
        "type": "string",
        "description": "An ice sculpture"
      },
      "price": {
        "type": "number",
        "description": "12.5"
      },
      "tags": {
        "description": "cold | ice", // meaningful description is added
        "type": "array",
        "items": {
          "description": "cold",
          "type": "string"
        }
      },
      "dimensions": {
        "type": "object",
        "description": "dimensions",
        "properties": {
          "length": {
            "type": "number",
            "description": "7"
          },
          "width": {
            "type": "integer",
            "description": "12"
          },
          "height": {
            "type": "number",
            "description": "9.5"
          }
        }
      },
      "warehouseLocation": {
        "type": "object",
        "description": "warehouseLocation",
        "properties": {
          "latitude": {
            "type": "number",
            "description": "-78.75"
          },
          "longitude": {
            "type": "number",
            "description": "20.4"
          }
        }
      }
    },
    "required": [
      "id",
      "name",
      "price",
      "dimensions",
      "warehouseLocation"
    ]
  }
}

There are two differences:

  1. Descriptions are added for every key and we try to make it meaningful as possible.
  2. id is always an integer and it's type is resolved to integer as we expected. Hooray 🎉.

Auto convert integer / float string to integer / number type 🍿

const obj = {
  integer1: 30000,
  integer2: '30000',
  integer3: '30000.00',
  float1: 30000.01,
  float2: '30000.01',
};

const schema = js2schema(obj, { title: 'my-card' });

output schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "my-card",
  "description": "my-card",
  "type": "object",
  "properties": {
    "integer1": {
      "type": "integer",
      "description": "30000"
    },
    "integer2": {
      "type": "integer",
      "description": "30000"
    },
    "integer3": {
      "type": "integer",
      "description": "30000.00"
    },
    "float1": {
      "type": "number",
      "description": "30000.01"
    },
    "float2": {
      "type": "number",
      "description": "30000.01"
    }
  }
}

And it can be turned off as your wish.

const schema = js2schema(input, { title: 'my-card', shouldConvertNumberString: false });

output schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "my-card",
  "description": "my-card",
  "type": "object",
  "properties": {
    "integer1": {
      "type": "integer",
      "description": "30000"
    },
    "integer2": {
      "type": "string",
      "description": "30000"
    },
    "integer3": {
      "type": "string",
      "description": "30000.00"
    },
    "float1": {
      "type": "number",
      "description": "30000.01"
    },
    "float2": {
      "type": "string",
      "description": "30000.01"
    }
  }
}

Only integer1 is integer and float1 is number, the rest are resolved to string as it is.

js2mySchema 🦄

The powerful feature is you can modify the type to whatever you want using typeResolvers, so you can build you own JavaScript type DSL ✍️.

// my-json-schema.js

const { js2schema, defaultResolvers } = require('js2schema');

/** @type {ITypeResolvers}  */
const typeResolvers = {
  image: {
    is: url => defaultResolvers.image.is(url) || isImageURL(url),
    type: () => 'Image',
  },
  url: {
    is: url => defaultResolvers.url.is(url) || isWechatURL(url),
    type: () => 'URL',
  },
  floatString: {
    type: () => 'Number',
  },
  integerString: {
    type: () => 'Number',
  },
  float: {
    type: () => 'Number',
  },
  integer: {
    type: () => 'Number',
  },
  string: {
    type: () => 'String',
  },
  text: {
    type: (value) => value.length >= 80 ? 'Text' : 'String',
  },
}

/**
 * @param {string} url
 * @returns {boolean}
 */
function isImageURL(src) {
  // Say we take https://inews.gtimg.com/newsapp_ls/0/13362798150_640330/0 as image url
  return src
    && typeof src === 'string'
    && ['https://inews.gtimg.com/'].some(prefix => src.startsWith(prefix))
  ;
}

/**
 * @param {string} url
 * @returns {boolean}
 */
function isWechatURL(url) {
  return url
    && typeof url === 'string'
    && ['wechat://'].some(prefix => url.startsWith(prefix))
  ;
}

/**
 * Convert json or js object or array to my json schema.
 * @param {object | any[]} jsObject
 * @param {string} [title] the schema title
 * @returns {string} json schema
 */
exports.js2mySchema = (jsObject, title = '') => {
  const mySchema = js2schema(jsObject, { title, typeResolvers });

  return mySchema;
}

Use js2mySchema:

const { js2mySchema } = require('./my-json-schema');

const input = {
  url1: 'wechat://pay',
  url2: 'https://stackoverflow.com/',
  i1: 1,
  f1: 1.1,
  s1: 'HelloWorld',
  t1: 'HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld',
  image1: 'https://json-schema.org/understanding-json-schema/_static/logo.ico',
  image2: 'https://inews.gtimg.com/newsapp_ls/0/13362798150_640330/0',
};

const actual = js2mySchema(input);
const expected = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "",
  "type": "object",
  "properties": {
    "url1": {
      "type": "URL",
      "description": "wechat://pay"
    },
    "url2": {
      "type": "URL",
      "description": "https://stackoverflow.com/"
    },
    "i1": {
      "type": "Number",
      "description": "1"
    },
    "f1": {
      "type": "Number",
      "description": "1.1"
    },
    "s1": {
      "type": "String",
      "description": "HelloWorld"
    },
    "t1": {
      "type": "Text",
      "description": "HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld"
    },
    "image1": {
      "type": "Image",
      "description": "https://json-schema.org/understanding-json-schema/_static/logo.ico"
    },
    "image2": {
      "type": "Image",
      "description": "https://inews.gtimg.com/newsapp_ls/0/13362798150_640330/0"
    }
  },
  "description": ""
};

As you can see, we have built our own type DSL JSON schema.

We created new types 🎉:

  • Image
  • URL
  • Number
  • String
  • Text

Run tests

npm test

Publish

npm version major / minor / patch && npm publish && gp && gp --tags

Author

👤 legend80s

能省一分钟是一分钟能给别人也省一分是真爱 ❤️

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator