Repository navigation
feat(concerto-core): add CollectionSizeValidator class with property gating and tests #1298
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
Merged
jamieshorten
merged 3 commits into
accordproject:main
from
muhabdulkadir:moh/core-introspect-collection-validator
Aug 25, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/concerto-core/src/introspect/collectionsizevalidator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const { isNull } = require('@accordproject/concerto-util').NullUtil; | ||
| const Validator = require('./validator'); | ||
|
|
||
| /** | ||
| * A Validator to enforce that a collection (array or map) has a size within a specified range. | ||
| * @private | ||
| * @class | ||
| * @memberof module:concerto-core | ||
| */ | ||
| class CollectionSizeValidator extends Validator { | ||
|
|
||
| /** | ||
| * Create a CollectionSizeValidator. | ||
| * @param {Object} field - the field or declarations this validator is attached to | ||
| * @param {Object} validator - The size validation object - [minSize, maxSize] (inclusive). | ||
| * | ||
| * @throws {IllegalModelException} | ||
| */ | ||
| constructor(field, validator) { | ||
| super(field, validator); | ||
| this.minSize = validator.minSize ?? null; | ||
| this.maxSize = validator.maxSize ?? null; | ||
|
|
||
| if (isNull(this.minSize) && isNull(this.maxSize)) { | ||
| this.reportError(field.getName(), 'Invalid collection size, minSize and/or maxSize must be specified.'); | ||
| } else if (this.minSize < 0 || this.maxSize < 0) { | ||
| this.reportError(field.getName(), 'minSize and/or maxSize must be positive integers.'); | ||
| } else if (isNull(this.minSize) || isNull(this.maxSize)) { | ||
| // this is fine and means that we don't need to check whether minSize > maxSize | ||
| } else if (this.minSize > this.maxSize) { | ||
| this.reportError(field.getName(), 'minSize must be less than or equal to maxSize.'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate the property | ||
| * @param {string} identifier the identifier of the instance being validated | ||
| * @param {number} value the collection size to validate | ||
| * @throws {IllegalModelException} | ||
| * @private | ||
| */ | ||
| validate(identifier, value) { | ||
| if(!isNull(this.minSize) && value < this.minSize) { | ||
| this.reportError(identifier, `Collection must contain at least ${this.minSize} elements.`); | ||
| } | ||
| if(!isNull(this.maxSize) && value > this.maxSize) { | ||
| this.reportError(identifier, `Collection must contain no more than ${this.maxSize} elements.`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the minSize for this validator, or null if not specified | ||
| * @returns {number} the min size or null | ||
| */ | ||
| getMinSize() { | ||
| return this.minSize; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the maxSize for this validator, or null if not specified | ||
| * @returns {number} the max size or null | ||
| */ | ||
| getMaxSize() { | ||
| return this.maxSize; | ||
| } | ||
|
|
||
| /** | ||
| * Determine if the validator is compatible with another validator. For the | ||
| * validators to be compatible, all values accepted by this validator must | ||
| * be accepted by the other validator. | ||
| * @param {Validator} other the other validator. | ||
| * @returns {boolean} True if this validator is compatible with the other | ||
| * validator, false otherwise. | ||
| */ | ||
| compatibleWith(other) { | ||
| if (!(other instanceof CollectionSizeValidator)) { | ||
| return false; | ||
| } | ||
|
|
||
| const thisMinSize = this.getMinSize(); | ||
| const otherMinSize = other.getMinSize(); | ||
| if (isNull(thisMinSize) && !isNull(otherMinSize)) { | ||
| return false; | ||
| } else if (!isNull(thisMinSize) && !isNull(otherMinSize)) { | ||
| if (thisMinSize < otherMinSize) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| const thisMaxSize = this.getMaxSize(); | ||
| const otherMaxSize = other.getMaxSize(); | ||
| if (isNull(thisMaxSize) && !isNull(otherMaxSize)) { | ||
| return false; | ||
| } else if (!isNull(thisMaxSize) && !isNull(otherMaxSize)) { | ||
| if (thisMaxSize > otherMaxSize) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| export = CollectionSizeValidator; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
packages/concerto-core/test/introspect/collectionsizevalidator.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const chai = require('chai'); | ||
| chai.should(); | ||
|
|
||
| const CollectionSizeValidator = require('../../src/introspect/collectionsizevalidator'); | ||
|
|
||
| describe('CollectionSizeValidator', () => { | ||
|
|
||
| const mockField = { | ||
| getName: () => 'testField', | ||
| getFullyQualifiedName: () => 'org.example.Thing.testField', | ||
| }; | ||
|
|
||
| describe('#constructor', () => { | ||
|
|
||
| it('should create with both minSize and maxSize', () => { | ||
| const v = new CollectionSizeValidator(mockField, { minSize: 1, maxSize: 10 }); | ||
| v.getMinSize().should.equal(1); | ||
| v.getMaxSize().should.equal(10); | ||
| }); | ||
|
|
||
| it('should create with minSize only', () => { | ||
| const v = new CollectionSizeValidator(mockField, { minSize: 3 }); | ||
| v.getMinSize().should.equal(3); | ||
| chai.expect(v.getMaxSize()).to.be.null; | ||
| }); | ||
|
|
||
| it('should create with maxSize only', () => { | ||
| const v = new CollectionSizeValidator(mockField, { maxSize: 5 }); | ||
| chai.expect(v.getMinSize()).to.be.null; | ||
| v.getMaxSize().should.equal(5); | ||
| }); | ||
|
|
||
| it('should throw when neither minSize nor maxSize specified', () => { | ||
| (() => new CollectionSizeValidator(mockField, {})) | ||
| .should.throw(/minSize and\/or maxSize must be specified/); | ||
| }); | ||
|
|
||
| it('should throw when minSize is negative', () => { | ||
| (() => new CollectionSizeValidator(mockField, { minSize: -1 })) | ||
| .should.throw(/positive integers/); | ||
| }); | ||
|
|
||
| it('should throw when maxSize is negative', () => { | ||
| (() => new CollectionSizeValidator(mockField, { maxSize: -2 })) | ||
| .should.throw(/positive integers/); | ||
| }); | ||
|
|
||
| it('should throw when minSize > maxSize', () => { | ||
| (() => new CollectionSizeValidator(mockField, { minSize: 5, maxSize: 2 })) | ||
| .should.throw(/minSize must be less than or equal to maxSize/); | ||
| }); | ||
|
|
||
| it('should allow minSize equal to maxSize', () => { | ||
| const v = new CollectionSizeValidator(mockField, { minSize: 3, maxSize: 3 }); | ||
| v.getMinSize().should.equal(3); | ||
| v.getMaxSize().should.equal(3); | ||
| }); | ||
|
|
||
| it('should allow minSize of zero', () => { | ||
| const v = new CollectionSizeValidator(mockField, { minSize: 0, maxSize: 5 }); | ||
| v.getMinSize().should.equal(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#validate', () => { | ||
|
|
||
| it('should pass when value is within bounds', () => { | ||
| const v = new CollectionSizeValidator(mockField, { minSize: 1, maxSize: 5 }); | ||
| v.validate('id', 1); | ||
| v.validate('id', 3); | ||
| v.validate('id', 5); | ||
| }); | ||
|
|
||
| it('should throw when value is below minSize', () => { | ||
| const v = new CollectionSizeValidator(mockField, { minSize: 2, maxSize: 5 }); | ||
| (() => v.validate('id', 1)).should.throw(/at least 2 elements/); | ||
| (() => v.validate('id', 0)).should.throw(/at least 2 elements/); | ||
| }); | ||
|
|
||
| it('should throw when value exceeds maxSize', () => { | ||
| const v = new CollectionSizeValidator(mockField, { minSize: 1, maxSize: 3 }); | ||
| (() => v.validate('id', 4)).should.throw(/no more than 3 elements/); | ||
| }); | ||
|
|
||
| it('should only enforce minSize when maxSize is null', () => { | ||
| const v = new CollectionSizeValidator(mockField, { minSize: 2 }); | ||
| v.validate('id', 100); | ||
| (() => v.validate('id', 1)).should.throw(/at least 2 elements/); | ||
| }); | ||
|
|
||
| it('should only enforce maxSize when minSize is null', () => { | ||
| const v = new CollectionSizeValidator(mockField, { maxSize: 3 }); | ||
| v.validate('id', 0); | ||
| (() => v.validate('id', 4)).should.throw(/no more than 3 elements/); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#compatibleWith', () => { | ||
|
|
||
| const validator = (opts) => new CollectionSizeValidator(mockField, opts); | ||
|
|
||
| it('should return false for non-CollectionSizeValidator', () => { | ||
| validator({ minSize: 1 }).compatibleWith({}).should.equal(false); | ||
| }); | ||
|
|
||
| it('should be compatible when other is more permissive (loosened)', () => { | ||
| validator({ minSize: 2, maxSize: 5 }) | ||
| .compatibleWith(validator({ minSize: 1, maxSize: 6 })) | ||
| .should.equal(true); | ||
| }); | ||
|
|
||
| it('should be incompatible when other tightens minSize', () => { | ||
| validator({ minSize: 1, maxSize: 5 }) | ||
| .compatibleWith(validator({ minSize: 3, maxSize: 5 })) | ||
| .should.equal(false); | ||
| }); | ||
|
|
||
| it('should be incompatible when other tightens maxSize', () => { | ||
| validator({ minSize: 1, maxSize: 5 }) | ||
| .compatibleWith(validator({ minSize: 1, maxSize: 3 })) | ||
| .should.equal(false); | ||
| }); | ||
|
|
||
| it('should be incompatible when this has no min and other adds one', () => { | ||
| validator({ maxSize: 10 }) | ||
| .compatibleWith(validator({ minSize: 1, maxSize: 10 })) | ||
| .should.equal(false); | ||
| }); | ||
|
|
||
| it('should be incompatible when this has no max and other adds one', () => { | ||
| validator({ minSize: 1 }) | ||
| .compatibleWith(validator({ minSize: 1, maxSize: 10 })) | ||
| .should.equal(false); | ||
| }); | ||
|
|
||
| it('should be compatible when other removes max', () => { | ||
| validator({ minSize: 1, maxSize: 5 }) | ||
| .compatibleWith(validator({ minSize: 1 })) | ||
| .should.equal(true); | ||
| }); | ||
|
|
||
| it('should be compatible when other removes min', () => { | ||
| validator({ minSize: 1, maxSize: 5 }) | ||
| .compatibleWith(validator({ maxSize: 5 })) | ||
| .should.equal(true); | ||
| }); | ||
|
|
||
| it('should be compatible with identical validator', () => { | ||
| validator({ minSize: 2, maxSize: 8 }) | ||
| .compatibleWith(validator({ minSize: 2, maxSize: 8 })) | ||
| .should.equal(true); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.