mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ACA-3742] Metadata - constraints validation (#5908)
* card min max value validator * card match value validator * card value length validator * map validators to constraint type * add minmax * update exported validators * register validators based on constraint type * translate errors with parameters * tests
This commit is contained in:
@@ -91,7 +91,7 @@ describe('CardViewBaseItemModel', () => {
|
||||
const isValid = itemModel.isValid('test-against-this');
|
||||
|
||||
expect(isValid).toBe(false);
|
||||
expect(itemModel.getValidationErrors('test-against-this')).toEqual(['validator 1', 'validator 3']);
|
||||
expect(itemModel.getValidationErrors('test-against-this')).toEqual([validator1, validator3 ]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
import { CardViewItemProperties, CardViewItemValidator } from '../interfaces/card-view.interfaces';
|
||||
import validatorsMap from '../validators/validators.map';
|
||||
|
||||
export abstract class CardViewBaseItemModel {
|
||||
label: string;
|
||||
@@ -38,6 +39,14 @@ export abstract class CardViewBaseItemModel {
|
||||
this.icon = cardViewItemProperties.icon || '';
|
||||
this.validators = cardViewItemProperties.validators || [];
|
||||
this.data = cardViewItemProperties.data || null;
|
||||
|
||||
if (cardViewItemProperties?.constraints?.length ?? 0) {
|
||||
for (const constraint of cardViewItemProperties.constraints) {
|
||||
if (constraint.type !== 'LIST') {
|
||||
this.validators.push(validatorsMap[constraint.type.toLowerCase()](constraint.parameters));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isEmpty(): boolean {
|
||||
@@ -54,11 +63,11 @@ export abstract class CardViewBaseItemModel {
|
||||
.reduce((isValidUntilNow, isValid) => isValidUntilNow && isValid, true);
|
||||
}
|
||||
|
||||
getValidationErrors(value): string[] {
|
||||
getValidationErrors(value): CardViewItemValidator[] {
|
||||
if (!this.validators.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.validators.filter((validator) => !validator.isValid(value)).map((validator) => validator.message);
|
||||
return this.validators.filter((validator) => !validator.isValid(value)).map((validator) => validator);
|
||||
}
|
||||
}
|
||||
|
@@ -56,4 +56,24 @@ describe('CardViewFloatItemModel', () => {
|
||||
expect(itemModel.isValid('42.3')).toBe(true, 'For "42.3" it should be true');
|
||||
expect(itemModel.isValid('test')).toBe(false, 'For "test" it should be false');
|
||||
});
|
||||
|
||||
it('should validate based on defined constraints', () => {
|
||||
const constrainedProperties = {
|
||||
label: 'Tribe',
|
||||
value: '42.42',
|
||||
key: 'tribe',
|
||||
dataType: 'd:float',
|
||||
constraints: [{
|
||||
id: 'constraint-id',
|
||||
type: 'MINMAX',
|
||||
parameters: { minValue: 10, maxValue: 40 }
|
||||
}]
|
||||
};
|
||||
|
||||
const itemModel = new CardViewFloatItemModel(constrainedProperties);
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(false, '42.42 is bigger than maximum allowed');
|
||||
|
||||
itemModel.value = '9.1';
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(false, '9.1 is less than minimum allowed');
|
||||
});
|
||||
});
|
||||
|
@@ -56,4 +56,24 @@ describe('CardViewIntItemModel', () => {
|
||||
expect(itemModel.isValid('42.3')).toBe(false, 'For "42.3" it should be false');
|
||||
expect(itemModel.isValid('test')).toBe(false, 'For "test" it should be false');
|
||||
});
|
||||
|
||||
it('should validate based on defined constraints', () => {
|
||||
const constrainedProperties = {
|
||||
label: 'Tribe',
|
||||
value: '20',
|
||||
key: 'tribe',
|
||||
dataType: 'd:float',
|
||||
constraints: [{
|
||||
id: 'constraint-id',
|
||||
type: 'MINMAX',
|
||||
parameters: { minValue: 10, maxValue: 15 }
|
||||
}]
|
||||
};
|
||||
|
||||
const itemModel = new CardViewIntItemModel(constrainedProperties);
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(false, '20 is bigger than maximum allowed');
|
||||
|
||||
itemModel.value = '5';
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(false, '5 is less than minimum allowed');
|
||||
});
|
||||
});
|
||||
|
@@ -85,4 +85,24 @@ describe('CardViewTextItemModel', () => {
|
||||
expect(itemModel.displayValue).toBe('testpiped-testpiped-testpiped-Banuk-1-2-3');
|
||||
});
|
||||
});
|
||||
|
||||
it('should validate based on defined constraints', () => {
|
||||
const constrainedProperties = {
|
||||
label: 'Tribe',
|
||||
value: 'test',
|
||||
key: 'tribe',
|
||||
dataType: 'd:text',
|
||||
constraints: [{
|
||||
id: 'constraint-id',
|
||||
type: 'REGEX',
|
||||
parameters: { expression: '^(?=.*test).*' }
|
||||
}]
|
||||
};
|
||||
|
||||
const itemModel = new CardViewTextItemModel(constrainedProperties);
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(true);
|
||||
|
||||
itemModel.value = 'dummy';
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(false, '`dummy` is not a constraint expression pattern');
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user