Writing schema magically
The name is somewhat of a poor choice, but it was available on npm.
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 };
export const jsonSchema = {
properties: {
foo: {
type: 'string',
},
bar: {
type: 'number',
exclusiveMinimum: 0,
},
baz: {},
qux: {
type: 'array',
items: {
type: 'string',
},
},
},
required: ['foo', 'baz'],
};
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,
},
],
});
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 };
MIT