bshamblen:json-schema-generator

v0.0.2Published 8 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

##JSON Schema Generator Generates a JSON-schema from a JSON document, or object. This is not intended to be bulletproof, since many of the attributes of the schema's properties are inferred from the data provided in the JSON document. It simply parses a JSON string and creates a best-guess representation of the JSON schema from the document.

Attribution

This library is based on the jsonschema/json-schema-v2 project (originally written for angular). This port is designed to work in any JavaScript based environment (web browsers, node.js, angular.js, Meteor...).

Web Browser

There are no prerequisites for this library.

The following file is located in the src folder of this repository:

1<script src="json-schema-generator.js"></script>
1//sample JSON string. Could also be populated from a textarea or file.
2var jsonString = '{"test" : "this"}';
3
4//create a new instance of the JSON Schema Generator
5var generator = new JSONSchemaGenerator({
6	url: 'http://example.com',
7	json: jsonString
8});
9
10//get the schema object
11var schemaObj = generator.getSchema();
12
13//get the formatted schema string.
14var schemaString = generator.getSchemaAsString(true);

Options

Options are passed to the JSONSchemaGenerator contructor as object properties.

optiondescriptiondefault
jsonThe JSON string or JSON object to be used when generating the JSON schema.{}
urlThe base URL for each property's id. Should not contain a trailing slash.http://example.com
arrayOptionsIndicates how you'd like the schemas for array values to be represented. See "ArrayOptions" below for more information.singleSchema
includeDefaultsIndicates whether or not you'd like to use the values from your JSON document as the default values in your schema.false
includeEnumsSimilar to includeDefault this option will use the values from the JSON document to define an email for every property, allowing only null and the value from the JSON document. You'll need to manually delete/modify the enums, based on your requirements.false
forceRequiredAdds a block of required properties at the bottom of the schema. All properties will be included. You'll need to reduce the list, based on your requirements.true
absoluteIdsPrepends the url value to the beginning each property id value.true
numericVerboseIncludes multipleOf, minimum, maximum, exclusiveMinimum, and exclusiveMaximim attriibutes for every property with a type of numeric. Attribute values will most likely need to be adjusted, based on your requirements.false
stringsVerboseIncludes the minLength attribute to every property with a type of string. Attribute values will most likely need to be adjusted, based on your requirements.false
objectsVerboseIncludes the "additionalProperties": true attribute to every property with a type of object. Attribute values may need to be adjusted, based on your requirements.false
arraysVerboseIncludes minItems, uniqueItems, and additionalItems atttibutes for every property with a type of array. Attribute values will most likely need to be adjusted, baed on your requirements.false
metadataKeywordsIncludes title, description, and name attributes for every property, with default text. The attributes will need to be modified to document each of the properties.false
additionalItemsIf set to false, includes the "additionalItems": false attribute for every property with a type of array.true
additionalPropertiesIf set to false, includes the "additionalProperties": false attribute for every property with a type of object.true

Contributions

Contributions are greatly appreciated. Please submit an issue first, to discuss the changes you intend to make, before submitting a pull request.