From e76bcc332965b8a45ef943e78721e145b31bceac Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Sat, 25 Feb 2017 23:59:20 +0200 Subject: [PATCH] feat: improved type string with minLength == maxLength closes #212 --- lib/services/schema-helper.service.spec.ts | 44 ++++++++++++++++++++++ lib/services/schema-helper.service.ts | 6 ++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/services/schema-helper.service.spec.ts b/lib/services/schema-helper.service.spec.ts index 46c43a491a..97e6de2a73 100644 --- a/lib/services/schema-helper.service.spec.ts +++ b/lib/services/schema-helper.service.spec.ts @@ -16,6 +16,50 @@ describe('Spec Helper', () => { expect(console.warn).toHaveBeenCalled(); (console.warn).and.callThrough(); }); + + describe('string', () => { + it('should calculate range for string with maxLength', () => { + let schema:any = { + type: 'string', + maxLength: 3 + }; + + SchemaHelper.runInjectors(schema, schema, '#/'); + schema._range.should.be.equal('<= 3 characters'); + }); + + it('should calculate range for string with minLength', () => { + let schema:any = { + type: 'string', + minLength: 3, + }; + + SchemaHelper.runInjectors(schema, schema, '#/'); + schema._range.should.be.equal('>= 3 characters'); + }); + + it('should calculate range for string with both max and minLength', () => { + let schema:any = { + type: 'string', + minLength: 3, + maxLength: 5 + }; + + SchemaHelper.runInjectors(schema, schema, '#/'); + schema._range.should.be.equal('[ 3 .. 5 ] characters'); + }); + + it('should calculate range for string with equal max and minLength', () => { + let schema:any = { + type: 'string', + minLength: 5, + maxLength: 5 + }; + + SchemaHelper.runInjectors(schema, schema, '#/'); + schema._range.should.be.equal('5 characters'); + }); + }); }); describe('preprocessProperties', () => { diff --git a/lib/services/schema-helper.service.ts b/lib/services/schema-helper.service.ts index fa6fd025eb..01ab1387bf 100644 --- a/lib/services/schema-helper.service.ts +++ b/lib/services/schema-helper.service.ts @@ -150,7 +150,11 @@ const injectors = { inject: (injectTo, propertySchema = injectTo) => { var range; if (propertySchema.minLength != undefined && propertySchema.maxLength != undefined) { - range = `[ ${propertySchema.minLength} .. ${propertySchema.maxLength} ]`; + if (propertySchema.minLength === propertySchema.maxLength) { + range = `${propertySchema.minLength}`; + } else { + range = `[ ${propertySchema.minLength} .. ${propertySchema.maxLength} ]`; + } } else if (propertySchema.maxLength != undefined) { range = '<= ' + propertySchema.maxLength; } else if (propertySchema.minLength != undefined) {