Skip to content

Commit

Permalink
[dsch] support for error messages in the field constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
DScheglov committed Jun 8, 2020
1 parent f5ad0e9 commit 9e30750
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 18 deletions.
3 changes: 3 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ const hasProperty = Function.prototype.call.bind(
Object.prototype.hasOwnProperty,
);

const readConstraint = constraint => (Array.isArray(constraint) ? constraint[0] : constraint);

module.exports = {
findPath,
ensurePath,
compose,
idX,
hasProperty,
readConstraint,
};
33 changes: 15 additions & 18 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = require('./config');
const [FIELDS_MAPPING] = require('./options/fieldOptionsMapping');
const { readConstraint } = require('./helpers');

module.exports = {
simpleType_jsonSchema,
Expand Down Expand Up @@ -106,28 +107,24 @@ function mixed_jsonSchema(name) {
return checkNullable(result);
}

const transformMatch = match => (
match instanceof RegExp
? match.toString().split('/').slice(1, -1).join('/')
: match.toString()
);

function __processOptions(t, type) {
t.__required = !!type.required;
t.__required = !!readConstraint(type.required);
if (type.enum && type.enum.slice) t.enum = type.enum.slice();
if (type.ref) t['x-ref'] = type.ref;
if (type.min != null) t.minimum = type.min;
if (type.max != null) t.maximum = type.max;
if (type.minLength != null) t.minLength = type.minLength;
if (type.maxLength != null) t.maxLength = type.maxLength;
if (type.minlength != null) t.minLength = type.minlength;
if (type.maxlength != null) t.maxLength = type.maxlength;
if (type.min != null) t.minimum = readConstraint(type.min);
if (type.max != null) t.maximum = readConstraint(type.max);
if (type.minLength != null) t.minLength = readConstraint(type.minLength);
if (type.maxLength != null) t.maxLength = readConstraint(type.maxLength);
if (type.minlength != null) t.minLength = readConstraint(type.minlength);
if (type.maxlength != null) t.maxLength = readConstraint(type.maxlength);
if (type.examples != null) t.examples = type.examples;
if (type.match) {
if (type.match instanceof RegExp) {
t.pattern = type.match
.toString()
.split('/')
.slice(1, -1)
.join('/');
} else {
t.pattern = type.match.toString();
}
}
if (type.match != null) t.pattern = transformMatch(readConstraint(type.match));
if (type.default !== undefined) t.default = type.default;

t.description = type.description || type.descr || type.ref && `Refers to ${type.ref}`;
Expand Down
94 changes: 94 additions & 0 deletions test/suites/translation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,61 @@ describe('schema.jsonSchema', () => {
});
});

it('should correctly translate number value constaints with error messages', () => {
const mSchema = new Schema({
value: {
type: Number,
min: [-5, 'Value shoule be greater or equal to -5'],
max: [50, 'Value should be less or equal to 50'],
required: [true, 'Value should be specified'],
default: 0,
},
}, { id: false, _id: false });

const jsonSchema = mSchema.jsonSchema('Sample');

assert.deepEqual(jsonSchema, {
title: 'Sample',
type: 'object',
properties: {
value: {
type: 'number',
minimum: -5,
maximum: 50,
default: 0,
},
},
required: ['value'],
});
});

it('should correctly translate number value constaints with error messages (not requires)', () => {
const mSchema = new Schema({
value: {
type: Number,
min: [-5, 'Value shoule be greater or equal to -5'],
max: [50, 'Value should be less or equal to 50'],
required: [false, 'Value is not required'],
default: 0,
},
}, { id: false, _id: false });

const jsonSchema = mSchema.jsonSchema('Sample');

assert.deepEqual(jsonSchema, {
title: 'Sample',
type: 'object',
properties: {
value: {
type: 'number',
minimum: -5,
maximum: 50,
default: 0,
},
},
});
});

it('should correctly translate string value constaints', () => {
const mSchema = new Schema({
valueFromList: {
Expand Down Expand Up @@ -617,6 +672,45 @@ describe('schema.jsonSchema', () => {
});
});

it('should correctly translate string value constaints with error message', () => {
const mSchema = new Schema({
valueFromList: {
type: String,
enum: ['red', 'green', 'yellow'],
required: true,
},
value20_30: {
type: String,
minLength: [20, 'Value should have at least 20 characters'],
maxLength: [30, 'Value should not be longer then 30 characters'],
},
value: { type: String, match: [/^(?:H|h)ello, .+$/, 'Value should start from greating'] },
}, { id: false, _id: false });

const jsonSchema = mSchema.jsonSchema('Sample');

assert.deepEqual(jsonSchema, {
title: 'Sample',
type: 'object',
properties: {
valueFromList: {
type: 'string',
enum: ['red', 'green', 'yellow'],
},
value20_30: {
type: 'string',
minLength: 20,
maxLength: 30,
},
value: {
type: 'string',
pattern: '^(?:H|h)ello, .+$',
},
},
required: ['valueFromList'],
});
});

it('should correctly translate string value constaints (minlength and maxlength)', () => {
const mSchema = new Schema({
valueFromList: {
Expand Down

0 comments on commit 9e30750

Please sign in to comment.