README
mongoose-schema-jsonschema
The module allows to create json schema from Mongoose schema by adding
jsonSchema
method to mongoose.Schema
, mongoose.Model
and mongoose.Query
classes
Contents
- Installation
- Schema Build Configuration
- Samples
- Validation tools
- Specifications
- Custom Schema Types Support
- Releases
- Supported Versions
Installation
npm install mongoose-schema-jsonschema
Schema Build Configuration
Since v1.4.0 it is able to configure how jsonSchema()
works.
To do that package was extended with config
function.
const config = require('mongoose-schema-jsonschema/config');
config({
// ... options go here
});
Currently there are two options that affects build process:
forceRebuild:
boolean
-- mongoose-schema-jsonschema caches json schemas built for mongoose schemas. That means we cannot built updated jsonSchema after some updates were made in the mongoose schema that already has jsonSchema. To resolve this issue theforceRebuild
was added (see sample bellow)fieldOptionsMapping:
{ [key: string]: string } | Array<string|[string, string]>
- allows to specify how to convert some custom options specified in the mongoose field definition.
const mongoose = require('mongoose-schema-jsonschema')();
const config = require('mongoose-schema-jsonschema/config');
const { Schema } = mongoose;
const BookSchema = new Schema({
title: { type: String, required: true, notes: 'Book Title' },
year: Number,
author: { type: String, required: true },
});
const fieldOptionsMapping = {
notes: 'x-notes',
};
config({ fieldOptionsMapping });
console.dir(BookSchema.jsonSchema(), { depth: null });
config({ fieldOptionsMapping: [], forceRebuild: true }); // reseting
console.dir(BookSchema.jsonSchema(), { depth: null });
Output:
{
type: 'object',
properties: {
title: { type: 'string', 'x-notes': 'Book Title' },
year: { type: 'number' },
author: { type: 'string' },
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}