Skip to content

Commit e76bcc3

Browse files
committed
feat: improved type string with minLength == maxLength
closes #212
1 parent a1aefa4 commit e76bcc3

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/services/schema-helper.service.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,50 @@ describe('Spec Helper', () => {
1616
expect(console.warn).toHaveBeenCalled();
1717
(<jasmine.Spy>console.warn).and.callThrough();
1818
});
19+
20+
describe('string', () => {
21+
it('should calculate range for string with maxLength', () => {
22+
let schema:any = {
23+
type: 'string',
24+
maxLength: 3
25+
};
26+
27+
SchemaHelper.runInjectors(schema, schema, '#/');
28+
schema._range.should.be.equal('<= 3 characters');
29+
});
30+
31+
it('should calculate range for string with minLength', () => {
32+
let schema:any = {
33+
type: 'string',
34+
minLength: 3,
35+
};
36+
37+
SchemaHelper.runInjectors(schema, schema, '#/');
38+
schema._range.should.be.equal('>= 3 characters');
39+
});
40+
41+
it('should calculate range for string with both max and minLength', () => {
42+
let schema:any = {
43+
type: 'string',
44+
minLength: 3,
45+
maxLength: 5
46+
};
47+
48+
SchemaHelper.runInjectors(schema, schema, '#/');
49+
schema._range.should.be.equal('[ 3 .. 5 ] characters');
50+
});
51+
52+
it('should calculate range for string with equal max and minLength', () => {
53+
let schema:any = {
54+
type: 'string',
55+
minLength: 5,
56+
maxLength: 5
57+
};
58+
59+
SchemaHelper.runInjectors(schema, schema, '#/');
60+
schema._range.should.be.equal('5 characters');
61+
});
62+
});
1963
});
2064

2165
describe('preprocessProperties', () => {

lib/services/schema-helper.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ const injectors = {
150150
inject: (injectTo, propertySchema = injectTo) => {
151151
var range;
152152
if (propertySchema.minLength != undefined && propertySchema.maxLength != undefined) {
153-
range = `[ ${propertySchema.minLength} .. ${propertySchema.maxLength} ]`;
153+
if (propertySchema.minLength === propertySchema.maxLength) {
154+
range = `${propertySchema.minLength}`;
155+
} else {
156+
range = `[ ${propertySchema.minLength} .. ${propertySchema.maxLength} ]`;
157+
}
154158
} else if (propertySchema.maxLength != undefined) {
155159
range = '<= ' + propertySchema.maxLength;
156160
} else if (propertySchema.minLength != undefined) {

0 commit comments

Comments
 (0)