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

fluent-schema-builder

v0.2.2

Published

Library for generating JSON Schema.

Downloads

1

Readme

json-schema-builder

Notice


This is a fork of the package JSON Schema builder that I created because I had a PR outstanding, with no comment, for over a year. Not expecting that to get merged.

NPM

Create syntactically correct JSON Schema with a fluent JavaScript API.

npm install --save json-schema-builder

See the wiki and tests for documentation and examples.

Basic Usage

Use json-schema-builder to create an empty schema object:

var jsb = require('json-schema-builder');
var schema = jsb.schema();

Use .json() on any JSON schema builder object to generate actual JSON Schema:

var doc = schema.json()

At this point, doc is just an empty schema ({}) that can be used by a JSON Schema validator to match any JSON instance (a JSON instance is any data in valid JSON format).

Add a validation constraint to any schema

Schemas can have validation constraints that restrict the set of JSON instances that can match a schema. There are constraints that apply to any schema.

One such constraint is type. For schemas that have a type constraint, there are additional constraints that can be applied depending on whether the type is numeric (number or integer), string, array, or object.

type constraint for any schema
var schema = jsb.schema().type( <value> )

where value is a string specifying any valid JSON Schema type (boolean, integer, number, string, array, object, and null).

Unless creating an empty schema as shown in the previous section, it is not necessary to explicitly invoke schema() as shown here. The following example shows the equivalent (and preferred) form:

var schema = jsb.type('string')

The type constraint can be used to restrict JSON instances to a particular set of acceptable types. The following example demonstrates how to specify a list of types that could be used to validate JSON instances that are either integer or string values:

var schema = jsb.type( 'integer', 'string' );

type has convenient wrappers corresponding to all the valid JSON Schema types:

var integerSchema = jsb.integer();  // jsb.type('integer')
var numberSchema  = jsb.number();   // jsb.type('number')
var booleanSchema = jsb.boolean();  // jsb.type('boolean')
var stringSchema  = jsb.string();   // jsb.type('string')
var arraySchema   = jsb.array();    // jsb.type('array')
var objectSchema  = jsb.object();   // jsb.type('object')
var nullSchema    = jsb.null();     // jsb.type('null')

Using integerSchema from this example, integerSchema.json() would generate the following JSON Schema document (or fragment):

{
  "type": "integer"
}

This schema can be used by a validator to match any integer JSON instance (any number without a fraction or exponent part).

Additional constraints for any schema

In addition to the type constraints, other constraints that can be applied to any schema include enum, allOf, anyOf, oneOf, and not.

See Validation for any instance type.

Constraints for numeric types

The following constraints can be applied to numeric types: multipleOf, maximum and exclusiveMaximum, and minimum and exclusiveMinimum.

See Validation for numeric types.

Constraints for string types

The following constraints can be applied to string types: maxLength, minLength, and pattern.

See Validation for string types.

Constraints for array types

The following constraints can be applied to array types: additionalItems and items, maxItems, minItems, and uniqueItems.

See Validation for array types.

Constraints for object types

The following constraints can be applied to object types: maxProperties, minProperties, required, additionalProperties, properties, patternProperties, and dependencies.

See Validation for object types.

Saving a schema to file

There is a convenience save function for saving a schema to a file. It generates output as JSON Schema and saves it as a UTF-8, formatted JSON file with 2-space indentation. In older versions, this was implemented as a method on the Schema class, but that restricted the library from being used in the browser (because of fs and path).

import save from 'fluent-schema-builder/dist/lib/save'

...

// save to a file synchronously
save(schema, path, to, filename);

// save to a file asynchronously
save(schema, filename, function(err) {
  ...
});

Of course, the output from schema.json() can be explicitly persisted any way desired.

Tests

npm test

json-schema-builder provides tests to verify the API can generate all the schemas that comprise the standard JSON Schema Test Suite.