json.schematic

1.0.0 • Public • Published

JSON.Schematic

🔎 Easy to use JSON schematics.

Usage

$ npm install json.schematic

Start by importing json.schematic into your project

const { Schematic } = require("json.schematic");

You can create schematics by outlining your desired object and setting the values as the types your want the values to be. You can use multiple types by using the | operator

const template = new Schematic({
  name: String,
  value: Number | String,
});

You can test objects to see if the fit into your Schematic by running Schematic.test

let data = {
  name: "my-name",
  value: 12,
}

console.log(template.test(data)); // true

data = {
  name: "my-other-name",
  value: "my-value",
}

console.log(template.test(data)); // true

data = {
  name: [ "not", "valid", "name" ],
  value: false,
}

console.log(template.test(data)); // false

You can also add parameters that can be set to any value by importing Any into your project

const { Schematic, Any } = require("json.schematic");

const template = new Schematic({
  value: Any,
});

const data = {
  value: "some-value",
}

console.log(template.test(data)); // true

If you want to allow more vales to be stored and only check values with the given keys, set the second argument to true when creating a Schematic

const template = new Schematic({
  name: String
}, true); // <--- HERE

const data = {
  name: "item",
  cost: 12,
  amount: 65128,
  id: "67akm2b5haw12"
}

console.log(template.test(data)); // true

Client Side

You do not need to run npm install, instead import the package from unpkg.com

<script type="module">
  import { Schematic } from "https://unpkg.com/json.schematic";
</script>

The package will work the same way

Package Sidebar

Install

npm i json.schematic

Weekly Downloads

1

Version

1.0.0

License

ISC

Unpacked Size

6.63 kB

Total Files

6

Last publish

Collaborators

  • ephf