Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add excludeFromJSONSchema option #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function schema_jsonSchema(name) {
schema_jsonSchema.__jsonSchemaIdCounter = 0;

function __build(name, schema) {

const paths = Object
.keys(schema.paths)
.filter(path => !/\.\$\*$/.test(path)); // removing Map.item paths
Expand All @@ -67,12 +68,14 @@ function __build(name, schema) {
for (path of paths) {
jss = ensurePath(result, path);
sch = schema.paths[path].jsonSchema(jss.prop);
jss.cont.properties[jss.prop] = sch;
if (sch.__required) {
jss.cont.required = jss.cont.required || [];
jss.cont.required.push(jss.prop);
if (!sch.excludeFromJSONSchema) {
jss.cont.properties[jss.prop] = sch;
if (sch.__required) {
jss.cont.required = jss.cont.required || [];
jss.cont.required.push(jss.prop);
}
delete sch.__required;
}
delete sch.__required;
}

if (result.required.length === 0) delete result.required;
Expand Down
3 changes: 3 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ function __processOptions(t, type) {
t.description = type.description || type.descr || type.ref && `Refers to ${type.ref}`;
if (!t.description) delete t.description;

t.excludeFromJSONSchema = type.excludeFromJSONSchema;
if (!t.excludeFromJSONSchema) delete t.excludeFromJSONSchema;

if (!t.title && type.title) t.title = type.title;

const customFieldsMapping = config.get(FIELDS_MAPPING);
Expand Down
64 changes: 64 additions & 0 deletions test/suites/exclude.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

const mongoose = require('../../index')(require('mongoose'));
const assert = require('assert');


describe('Exclude: Schema.jsonSchema()', () => {
it('should exclude fields marked with excludeFromJSONSchema = true', () => {

const mSchema = new mongoose.Schema({
s: {
type: String,
excludeFromJSONSchema: true,
},
c: {
type: String,
required: true,
}
});

const jsonSchema = mSchema.jsonSchema();

assert.equal(jsonSchema.properties.s, undefined);
});



it('should not exclude fields with excludeFromJSONSchema = false', () => {

const mSchema = new mongoose.Schema({
s: {
type: String,
excludeFromJSONSchema: false,
},
c: {
type: String,
required: true,
}
});

const jsonSchema = mSchema.jsonSchema();
assert.ok(!!jsonSchema.properties.s)

});

it('should not exclude fields with !excludeFromJSONSchema', () => {

const mSchema = new mongoose.Schema({
s: {
type: String,
excludeFromJSONSchema: false,
},
c: {
type: String,
required: true,
}
});

const jsonSchema = mSchema.jsonSchema();
assert.ok(!!jsonSchema.properties.c)

});


});