Skip to content

Commit

Permalink
[ACS-6586] "Folder" property complains about letters provided into "N…
Browse files Browse the repository at this point in the history
…ame" input field (#9365)
  • Loading branch information
jacekpluta authored Mar 5, 2024
1 parent a114459 commit 77a87f0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
58 changes: 58 additions & 0 deletions lib/core/src/lib/card-view/models/card-view-baseitem.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,63 @@ describe('CardViewBaseItemModel', () => {
expect(isValid).toBe(false);
expect(itemModel.getValidationErrors('test-against-this')).toEqual([validator1, validator3 ]);
});

it('should validate field with special character and return false when there is REGEX constraint and requiresMatch is false', () => {
const constrainedProperties: CardViewItemProperties = {
...properties,
value: 'test.',
constraints: [{
id: 'constraint-id',
type: 'REGEX',
parameters: { expression: '(.*[\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)', requiresMatch: false }
}]
};

const itemModel = new CarViewCustomItemModel(constrainedProperties);
expect(itemModel.isValid(itemModel.value)).toBe(false);
});

it('should validate field without special character and return true when there is REGEX constraint and requiresMatch is false', () => {
const constrainedProperties: CardViewItemProperties = {
...properties,
constraints: [{
id: 'constraint-id',
type: 'REGEX',
parameters: { expression: '(.*[\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)', requiresMatch: false }
}]
};

const itemModel = new CarViewCustomItemModel(constrainedProperties);
expect(itemModel.isValid(itemModel.value)).toBe(true);
});

it('should validate field without special character and return false when there is REGEX constraint and requiresMatch is true', () => {
const constrainedProperties: CardViewItemProperties = {
...properties,
constraints: [{
id: 'constraint-id',
type: 'REGEX',
parameters: { expression: '(.*[\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)', requiresMatch: true }
}]
};

const itemModel = new CarViewCustomItemModel(constrainedProperties);
expect(itemModel.isValid(itemModel.value)).toBe(false);
});

it('should validate field without special character and return true when there is REGEX constraint and requiresMatch is true', () => {
const constrainedProperties: CardViewItemProperties = {
...properties,
value: 'test.',
constraints: [{
id: 'constraint-id',
type: 'REGEX',
parameters: { expression: '(.*[\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)', requiresMatch: true }
}]
};

const itemModel = new CarViewCustomItemModel(constrainedProperties);
expect(itemModel.isValid(itemModel.value)).toBe(true);
});
});
});
26 changes: 22 additions & 4 deletions lib/core/src/lib/card-view/models/card-view-textitem.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,15 @@ describe('CardViewTextItemModel', () => {
});
});

it('should validate based on defined constraints', () => {
const constrainedProperties = {
it('should validate based on defined constraints and require a match to be valid', () => {
const constrainedProperties: CardViewTextItemProperties = {
label: 'Tribe',
value: 'test',
key: 'tribe',
dataType: 'd:text',
constraints: [{
id: 'constraint-id',
type: 'REGEX',
parameters: { expression: '^(?=.*test).*' }
parameters: { expression: '^(?=.*test).*', requiresMatch: true }
}]
};

Expand All @@ -105,4 +104,23 @@ describe('CardViewTextItemModel', () => {
itemModel.value = 'dummy';
expect(itemModel.isValid(itemModel.value)).toBe(false, '`dummy` is not a constraint expression pattern');
});

it('should validate based on defined constraints and not require a match to be valid', () => {
const constrainedProperties: CardViewTextItemProperties = {
label: 'Tribe',
value: 'test',
key: 'tribe',
constraints: [{
id: 'constraint-id',
type: 'REGEX',
parameters: { expression: '^(?=.*test).*', requiresMatch: false }
}]
};

const itemModel = new CardViewTextItemModel(constrainedProperties);
expect(itemModel.isValid(itemModel.value)).toBe(false);

itemModel.value = 'dummy';
expect(itemModel.isValid(itemModel.value)).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import { CardViewItemValidator } from '../interfaces/card-view.interfaces';
export interface MatchValidatorParams {
expression: string;
flags?: string;
requiresMatch?: boolean;
}

export class CardViewItemMatchValidator implements CardViewItemValidator {
message = 'CORE.CARDVIEW.VALIDATORS.MATCH_VALIDATION_ERROR';

constructor(private expression: string, private flags?: string) {}
constructor(private expression: string, private flags?: string, private requiresMatch?: boolean) {
}

isValid(value: string): boolean {
const regex = new RegExp(this.expression, this.flags);
return value === '' || regex.test(value);
return value === '' || this.requiresMatch ? regex.test(value) : !regex.test(value);
}
}
2 changes: 1 addition & 1 deletion lib/core/src/lib/card-view/validators/validators.map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CardViewItemLengthValidator, LengthValidatorParams } from './card-view-

const validators = {
minmax: (parameters: MinMaxValidatorParams) => new CardViewItemMinMaxValidator(parameters.minValue, parameters.maxValue),
regex: (parameters: MatchValidatorParams) => new CardViewItemMatchValidator(parameters.expression),
regex: (parameters: MatchValidatorParams) => new CardViewItemMatchValidator(parameters.expression, parameters.flags, parameters.requiresMatch),
length: (parameters: LengthValidatorParams) => new CardViewItemLengthValidator(parameters.minLength, parameters.maxLength)
};
export default validators;

0 comments on commit 77a87f0

Please sign in to comment.