Skip to content

Commit 8df15d2

Browse files
khaledGadelhaQKhaled Gadelhaq
andauthored
Fixing Default Values Ignoring Meta properties. (#1013)
* Validating defaultValue against range constriants for numbers Signed-off-by: Khaled Gadelhaq <[email protected]> * Validating defaultValue against regex and length constriants and fixing an invalid regex Signed-off-by: Khaled Gadelhaq <[email protected]> * Fixing concerto-cto tests due the recent changes Signed-off-by: Khaled Gadelhaq <[email protected]> --------- Signed-off-by: Khaled Gadelhaq <[email protected]> Co-authored-by: Khaled Gadelhaq <[email protected]>
1 parent f119fe5 commit 8df15d2

File tree

11 files changed

+87
-7
lines changed

11 files changed

+87
-7
lines changed

packages/concerto-core/lib/introspect/numbervalidator.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ class NumberValidator extends Validator{
6565
this.reportError(null, 'Lower bound must be less than or equal to upper bound.');
6666
}
6767
}
68+
69+
if(this.field?.ast?.defaultValue !== undefined) {
70+
let value = this.field.ast.defaultValue;
71+
if(this.lowerBound !== null && value < this.lowerBound) {
72+
this.reportError(null, `Value ${value} is outside lower bound ${this.lowerBound}`);
73+
}
74+
75+
if(this.upperBound !== null && value > this.upperBound) {
76+
this.reportError(null, `Value ${value} is outside upper bound ${this.upperBound}`);
77+
}
78+
}
6879
}
6980

7081
/**

packages/concerto-core/lib/introspect/stringvalidator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class StringValidator extends Validator{
7474
this.reportError(field.getName(), exception.message, ErrorCodes.REGEX_VALIDATOR_EXCEPTION);
7575
}
7676
}
77+
78+
if(this.field?.ast?.defaultValue) {
79+
this.validate(field.getName(), this.field.ast.defaultValue);
80+
}
7781
}
7882

7983
/**

packages/concerto-core/test/data/model/dependencies/base/base.cto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ concept Address {
6868
o String postOfficeBoxNumber optional
6969
}
7070

71-
scalar SSN extends String default="000-00-0000" regex=/\d{3}-\d{2}-\{4}+/
71+
scalar SSN extends String default="000-00-0000" regex= /\d{3}-\d{2}-\d{4}/

packages/concerto-core/test/data/parser/classdeclaration.scalararray.cto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace com.testing
1616

17-
scalar SSN extends String default="000-00-0000" regex=/\d{3}-\d{2}-\{4}+/
17+
scalar SSN extends String default="000-00-0000" regex= /\d{3}-\d{2}-\d{4}/
1818

1919
concept Persons {
2020
o SSN[] ssnArray

packages/concerto-core/test/data/parser/classdeclaration.scalaridentifier.cto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace com.testing
1616

17-
scalar SSN extends String default="000-00-0000" regex=/\d{3}-\d{2}-\{4}+/
17+
scalar SSN extends String default="000-00-0000" regex=/\d{3}-\d{2}-\d{4}/
1818

1919
concept Person identified by ssn {
2020
o SSN ssn

packages/concerto-core/test/data/parser/scalardeclaration.ssn.cto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414

1515
namespace com.testing
1616

17-
scalar SSN extends String default="000-00-0000" regex=/\d{3}-\d{2}-\{4}+/
17+
scalar SSN extends String default="000-00-0000" regex= /\d{3}-\d{2}-\d{4}/
1818
scalar BoolWithDefault extends Boolean default=false

packages/concerto-core/test/introspect/numbervalidator.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,41 @@ describe('NumberValidator', () => {
7171
new NumberValidator(mockField, LOWER_IS_HIGHER_THAN_UPPER);
7272
}).should.throw(/Lower bound must be less than or equal to upper bound./);
7373
});
74+
75+
it('should use default value if input is null and within range', () => {
76+
mockField.ast = { defaultValue: 50 };
77+
(() => {
78+
new NumberValidator(mockField, { lower: 0, upper: 100 });
79+
}).should.not.throw();
80+
});
81+
82+
it('should throw an error if default value is outside the lower bound', () => {
83+
mockField.ast = { defaultValue: 5 };
84+
(() => {
85+
new NumberValidator(mockField, { lower: 10, upper: 100 });
86+
}).should.throw(/Value 5 is outside lower bound 10/);
87+
});
88+
89+
it('should throw an error if default value is outside the upper bound', () => {
90+
mockField.ast = { defaultValue: 60 };
91+
(() => {
92+
new NumberValidator(mockField, { lower: 0, upper: 50 });
93+
}).should.throw(/Value 60 is outside upper bound 50/);
94+
});
95+
96+
it('should not throw an error if default value is exactly at the lower bound', () => {
97+
mockField.ast = { defaultValue: 10 };
98+
(() => {
99+
new NumberValidator(mockField, { lower: 10, upper: 100 });
100+
}).should.not.throw();
101+
});
102+
103+
it('should not throw an error if default value is exactly at the upper bound', () => {
104+
mockField.ast = { defaultValue: 50 };
105+
(() => {
106+
new NumberValidator(mockField, { lower: 0, upper: 50 });
107+
}).should.not.throw();
108+
});
74109
});
75110

76111
describe('#validate', () => {

packages/concerto-core/test/introspect/scalardeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe('ScalarDeclaration', () => {
144144
});
145145
it('should return the validator', () => {
146146
const testClass = modelManager.getType('com.testing.SSN');
147-
should.equal(testClass.getValidator().validator.pattern, '\\d{3}-\\d{2}-\\{4}+');
147+
should.equal(testClass.getValidator().validator.pattern, '\\d{3}-\\d{2}-\\d{4}');
148148
});
149149
});
150150

packages/concerto-core/test/introspect/stringvalidator.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,36 @@ describe('StringValidator', () => {
8181
new StringValidator(mockField, null, NEGETIVE_LENGTH);
8282
}).should.throw(/minLength and-or maxLength must be positive integers/);
8383
});
84+
85+
it('should throw when defaultValue length is less than minLength', () => {
86+
mockField.ast = { defaultValue: 'abc' };
87+
(() => {
88+
new StringValidator(mockField, null, {
89+
minLength: 5,
90+
maxLength: 10,
91+
});
92+
}).should.throw(/The string length of 'abc' should be at least 5 characters./);
93+
});
94+
95+
it('should throw when defaultValue length is greater than maxLength', () => {
96+
mockField.ast = { defaultValue: 'abcdefgh' };
97+
(() => {
98+
new StringValidator(mockField, null, {
99+
minLength: 2,
100+
maxLength: 5,
101+
});
102+
}).should.throw(/The string length of 'abcdefgh' should not exceed 5 characters./);
103+
});
104+
105+
it('should accept valid defaultValue that matches length and pattern constraints', () => {
106+
mockField.ast = { defaultValue: 'ABC' };
107+
(()=>{
108+
new StringValidator(mockField,
109+
{ pattern: '^[A-Z]{3,5}$' },
110+
{ minLength: 3, maxLength: 5 },
111+
);
112+
}).should.not.throw();
113+
});
84114
});
85115

86116
describe('#validate', () => {

packages/concerto-cto/test/cto/scalar.cto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ concept ScalarsWithFieldMetaProperties {
6969
o StringScalarWithDefaultAndRegex[] stringScalarWithDefaultAndRegexOptionalArray optional
7070
}
7171

72-
scalar SSN extends String default="000-00-0000" regex=/\d{3}-\d{2}-\{4}+/ length=[1,100]
72+
scalar SSN extends String default="000-00-0000" regex=/\d{3}-\d{2}-\d{4}/ length=[1,100]
7373

7474
concept Person identified by ssn {
7575
o SSN ssn

0 commit comments

Comments
 (0)