skeeler

0.1.0 • Public • Published

skeeler

Build Status Coverage Status

[WIP] Writing schema magically

The name is somewhat of a poor choice, but it was available on npm.

Simple Example

import Skeeler from 'skeeler';
import SkeelerJSONSchemaDraft6 from 'skeeler-json-schema-draft-6';
import SkeelerMongoose from 'skeeler-mongoose';
 
const types = Skeeler.use('json', new SkeelerJSONSchemaDraft6())
  .use('mongoose', new SkeelerMongoose())
  .getKeywords();
 
const mySkeeler = new Skeeler({
  foo: types.string.required.unique,
  bar: types.number.index.exclusiveMinimum(0),
  baz: types.objectId.required,
  qux: types.array(types.string),
});
 
const jsonSchema = mySkeeler.export('json');
const mongooseSchema = mySkeeler.export('mongoose');
 
export { jsonSchema, mongooseSchema };

Equals to JSON Schema v6

export const jsonSchema = {
  properties: {
    foo: {
      type: 'string',
    },
    bar: {
      type: 'number',
      exclusiveMinimum: 0,
    },
    baz: {},
    qux: {
      type: 'array',
      items: {
        type: 'string',
      },
    },
  },
  required: ['foo', 'baz'],
};

Equals to Mongoose Schema

export const mongooseSchema = new Mongoose.Schema({
  foo: {
    type: String,
    required: true,
    unique: true,
  },
  bar: {
    type: Number,
    index: true,
  },
  baz: {
    type: Mongoose.Types.ObjectId,
    required: true,
  },
  qux: [
    {
      type: String,
    },
  ],
});

Complex Example

import Skeeler from 'skeeler';
import SkeelerJSONSchemaDraft6 from 'skeeler-json-schema-draft-6';
import SkeelerMongoose from 'skeeler-mongoose';
 
const types = Skeeler.use('json', new SkeelerJSONSchemaDraft6())
  .use('mongoose', new SkeelerMongoose())
  .getKeywords();
 
const mySkeeler = new Skeeler({
  foo: types.string.required.unique,
  bar: types.number.index,
  baz: types.objectId.required,
  qux: types.anyOf([
    types.object({
      quux: types.number.exclusiveMinimum(0),
      corge: types.string,
    }),
    types.string.enum(['grault', 'garply']),
    types.boolean,
  ]),
  waldo: types.anyOf([types.array(types.string), types.string]).default([]),
});
 
const jsonSchema = mySkeeler.export('json');
 
const mongooseSchema = mySkeeler.export('mongoose', { timestamps: true });
mongooseSchema.index({ foo: 'text', baz: 'text' });
 
export { jsonSchema, mongooseSchema };

Related Projects

License

MIT

Package Sidebar

Install

npm i skeeler

Weekly Downloads

2

Version

0.1.0

License

MIT

Last publish

Collaborators

  • cap32