bshamblen:json-simple-schema

v0.1.2Published 10 years ago

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

Build Status

JSON Schema to SimpleSchema Converter

Converts a JSON schema document to a SimpleSchema object, for use with Collection2 and AutoForm.

Install

meteor add bshamblen:json-simple-schema

Use

Simply load the contents of your JSON schema document from your local file system, or from a URL, and pass the parsed JSON object to the JSONSchema constructor:

toSimpleSchema

Generates an instance of SimpleSchema, ready to attach to your Meteor collection.

1var jsonSchemaDoc = JSON.parse($.ajax({
2	type: 'GET',
3	url: 'http://example.com/path-to-json-schema-file',
4	async: false
5}).responseText);
6
7var jsonSchema = new JSONSchema(jsonSchemaDoc);
8var simpleSchema = jsonSchema.toSimpleSchema();
9
10YourModel = new Mongo.Collection('somecollection');
11YourModel.attachSchema(simpleSchema);

toSimpleSchemaProps

Generates just the schema properties object, which can be modified prior to manually creating an instance of SimpleSchema.

1var jsonSchemaDoc = JSON.parse($.ajax({
2	type: 'GET',
3	url: 'http://example.com/path-to-json-schema-file',
4	async: false
5}).responseText);
6
7var jsonSchema = new JSONSchema(jsonSchemaDoc);
8var props = jsonSchema.toSimpleSchemaProps();
9
10props.extraProperty = {
11	type: String,
12	optional: true
13}
14
15var simpleSchema = new SimpleSchema(props);
16
17YourModel = new Mongo.Collection('somecollection');
18YourModel.attachSchema(simpleSchema);

Disclaimer

This is the first iteration of this project, with minimal functionality. It currently supports base data types (including arrays), inline sub-objects and many of the validation options:

  • title
  • minimum
  • maximum
  • exclusiveMinimum
  • exclusiveMaximum
  • minLength
  • maxLength
  • enum
  • minItems
  • maxItems
  • default
  • pattern
  • required
  • Now supports internal $ref (thanks @bpatridge)

TODO

  • Add support for external $ref schemas, from a URI.

Contributing

Please feel free to contribute by sumbitting a pull request.