mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[MNT-23194] add long validators, correct filename typos (#9799)
* MNT-23194 add long validators, correct filename typos * MNT-23194 improve unit tests, fix naming typos * MNT-23194 change method name to more precise * MNT-23194 remove redundant type definitions, change any type, add different notation integers parsing
This commit is contained in:
committed by
GitHub
parent
4d2c489508
commit
6b8c064536
@@ -135,8 +135,12 @@ export class CardViewTextItemComponent extends BaseCardView<CardViewTextItemMode
|
||||
}
|
||||
|
||||
prepareValueForUpload(property: CardViewTextItemModel, value: string | string[]): string | string[] {
|
||||
if (property.multivalued && typeof value === 'string') {
|
||||
return value.split(this.multiValueSeparator.trim()).map((item) => item.trim());
|
||||
if (typeof value === 'string') {
|
||||
if (property.multivalued) {
|
||||
return value.split(this.multiValueSeparator.trim()).map((item) => item.trim());
|
||||
} else if (property.type === 'int' || property.type === 'long') {
|
||||
return this.prepareIntLongValue(value);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -151,11 +155,14 @@ export class CardViewTextItemComponent extends BaseCardView<CardViewTextItemMode
|
||||
|
||||
addValueToList(newListItem: MatChipInputEvent) {
|
||||
const chipInput = newListItem.chipInput.inputElement;
|
||||
const chipValue = newListItem.value.trim() || '';
|
||||
let chipValue = newListItem.value.trim() || '';
|
||||
|
||||
if (typeof this.editedValue !== 'string') {
|
||||
if (this.property.isValid(chipValue)) {
|
||||
if (chipValue) {
|
||||
if (this.property.type === 'int' || this.property.type === 'long') {
|
||||
chipValue = this.prepareIntLongValue(chipValue);
|
||||
}
|
||||
this.editedValue.push(chipValue);
|
||||
this.update();
|
||||
}
|
||||
@@ -223,4 +230,8 @@ export class CardViewTextItemComponent extends BaseCardView<CardViewTextItemMode
|
||||
get showLabelForChips(): boolean {
|
||||
return this.displayLabelForChips;
|
||||
}
|
||||
|
||||
private prepareIntLongValue(value: string): string {
|
||||
return String(Math.trunc(Number(value)));
|
||||
}
|
||||
}
|
||||
|
@@ -21,8 +21,8 @@ import { CardViewBaseItemModel } from './card-view-baseitem.model';
|
||||
import { CardViewBoolItemProperties } from '../interfaces/card-view.interfaces';
|
||||
|
||||
export class CardViewBoolItemModel extends CardViewBaseItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'bool';
|
||||
value: boolean = false;
|
||||
type = 'bool';
|
||||
value = false;
|
||||
default: boolean;
|
||||
|
||||
constructor(cardViewBoolItemProperties: CardViewBoolItemProperties) {
|
||||
|
@@ -25,7 +25,7 @@ import { DateFnsUtils } from '../../common/utils/date-fns-utils';
|
||||
type DateItemType = Date | Date[] | null;
|
||||
|
||||
export class CardViewDateItemModel extends CardViewBaseItemModel<DateItemType> implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'date';
|
||||
type = 'date';
|
||||
format: string;
|
||||
locale: string;
|
||||
|
||||
@@ -51,9 +51,7 @@ export class CardViewDateItemModel extends CardViewBaseItemModel<DateItemType> i
|
||||
return this.default ? [this.default] : [];
|
||||
}
|
||||
} else {
|
||||
return this.value && !Array.isArray(this.value)
|
||||
? this.transformDate(this.prepareDate(this.value))
|
||||
: this.default;
|
||||
return this.value && !Array.isArray(this.value) ? this.transformDate(this.prepareDate(this.value)) : this.default;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -21,8 +21,8 @@ import { CardViewDateItemModel } from './card-view-dateitem.model';
|
||||
import { CardViewDateItemProperties } from '../interfaces/card-view.interfaces';
|
||||
|
||||
export class CardViewDatetimeItemModel extends CardViewDateItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'datetime';
|
||||
format: string = 'MMM d, y, H:mm';
|
||||
type = 'datetime';
|
||||
format = 'MMM d, y, H:mm';
|
||||
|
||||
constructor(cardViewDateItemProperties: CardViewDateItemProperties) {
|
||||
super(cardViewDateItemProperties);
|
||||
|
@@ -22,8 +22,8 @@ import { CardViewTextItemProperties } from '../interfaces/card-view.interfaces';
|
||||
import { CardViewItemFloatValidator } from '../validators/card-view.validators';
|
||||
|
||||
export class CardViewFloatItemModel extends CardViewTextItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'float';
|
||||
inputType: string = 'number';
|
||||
type = 'float';
|
||||
inputType = 'number';
|
||||
|
||||
constructor(cardViewTextItemProperties: CardViewTextItemProperties) {
|
||||
super(cardViewTextItemProperties);
|
||||
|
@@ -22,8 +22,8 @@ import { CardViewIntItemProperties } from '../interfaces/card-view.interfaces';
|
||||
import { CardViewItemIntValidator, CardViewItemPositiveIntValidator } from '../validators/card-view.validators';
|
||||
|
||||
export class CardViewIntItemModel extends CardViewTextItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'int';
|
||||
inputType: string = 'number';
|
||||
type = 'int';
|
||||
inputType = 'number';
|
||||
|
||||
constructor(cardViewIntItemProperties: CardViewIntItemProperties) {
|
||||
super(cardViewIntItemProperties);
|
||||
|
@@ -21,7 +21,7 @@ import { CardViewBaseItemModel } from './card-view-baseitem.model';
|
||||
import { CardViewKeyValuePairsItemProperties } from '../interfaces/card-view.interfaces';
|
||||
|
||||
export class CardViewKeyValuePairsItemModel extends CardViewBaseItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'keyvaluepairs';
|
||||
type = 'keyvaluepairs';
|
||||
|
||||
constructor(cardViewKeyValuePairsItemProperties: CardViewKeyValuePairsItemProperties) {
|
||||
super(cardViewKeyValuePairsItemProperties);
|
||||
|
@@ -0,0 +1,83 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { CardViewLongItemModel } from './card-view-longitem.model';
|
||||
import { CardViewTextItemProperties } from '../interfaces/card-view.interfaces';
|
||||
|
||||
describe('CardViewLongItemModel', () => {
|
||||
let properties: CardViewTextItemProperties;
|
||||
|
||||
beforeEach(() => {
|
||||
properties = {
|
||||
label: 'Long Number',
|
||||
value: '21',
|
||||
key: 'long'
|
||||
};
|
||||
});
|
||||
|
||||
it('value should be parsed as a long integer', () => {
|
||||
const itemModel = new CardViewLongItemModel(properties);
|
||||
|
||||
expect(itemModel.value).toBe(21);
|
||||
});
|
||||
|
||||
it('value should be parsed as a long integer only if there is a value', () => {
|
||||
properties.value = undefined;
|
||||
const itemModel = new CardViewLongItemModel(properties);
|
||||
|
||||
expect(itemModel.value).toBe(undefined);
|
||||
});
|
||||
|
||||
it('isValid should return the validators value', () => {
|
||||
const itemModel = new CardViewLongItemModel(properties);
|
||||
|
||||
expect(itemModel.isValid(21)).toBe(true, 'For 21 it should be true');
|
||||
expect(itemModel.isValid(21.0)).toBe(true, 'For 21.0 it should be true');
|
||||
expect(itemModel.isValid('21')).toBe(true, 'For "21" it should be true');
|
||||
expect(itemModel.isValid('21.0')).toBe(true, 'For "21.0" it should be true');
|
||||
expect(itemModel.isValid('2e1')).toBe(true, 'For "2e1" it should be true');
|
||||
expect(itemModel.isValid('2g1')).toBe(false, 'For "2g1" it should be false');
|
||||
expect(itemModel.isValid(21.3)).toBe(false, 'For 21.3 it should be false');
|
||||
expect(itemModel.isValid('21.3')).toBe(false, 'For "21.3" it should be false');
|
||||
expect(itemModel.isValid('text')).toBe(false, 'For "text" it should be false');
|
||||
});
|
||||
|
||||
it('should validate based on defined constraints', () => {
|
||||
const constrainedProperties = {
|
||||
label: 'Some Number',
|
||||
value: '21',
|
||||
key: 'number',
|
||||
dataType: 'd:float',
|
||||
constraints: [
|
||||
{
|
||||
id: 'constraint-id',
|
||||
type: 'MINMAX',
|
||||
parameters: { minValue: 10, maxValue: 15 }
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const itemModel = new CardViewLongItemModel(constrainedProperties);
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(false, '21 is bigger than maximum allowed');
|
||||
|
||||
itemModel.value = '5';
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(false, '5 is less than minimum allowed');
|
||||
|
||||
itemModel.value = '13';
|
||||
expect(itemModel.isValid(itemModel.value)).toBe(true, '13 is within the allowed range');
|
||||
});
|
||||
});
|
@@ -0,0 +1,45 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { CardViewItem } from '../interfaces/card-view-item.interface';
|
||||
import { DynamicComponentModel } from '../../common/services/dynamic-component-mapper.service';
|
||||
import { CardViewTextItemModel } from './card-view-textitem.model';
|
||||
import { CardViewIntItemProperties } from '../interfaces/card-view.interfaces';
|
||||
import { CardViewItemLongValidator, CardViewItemPositiveLongValidator } from '../validators/card-view.validators';
|
||||
|
||||
export class CardViewLongItemModel extends CardViewTextItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type = 'long';
|
||||
inputType = 'number';
|
||||
|
||||
constructor(cardViewIntItemProperties: CardViewIntItemProperties) {
|
||||
super(cardViewIntItemProperties);
|
||||
|
||||
this.validators.push(new CardViewItemLongValidator());
|
||||
|
||||
if (cardViewIntItemProperties.allowOnlyPositiveNumbers) {
|
||||
this.validators.push(new CardViewItemPositiveLongValidator());
|
||||
}
|
||||
|
||||
if (cardViewIntItemProperties.value && !cardViewIntItemProperties.multivalued) {
|
||||
this.value = Number(cardViewIntItemProperties.value);
|
||||
}
|
||||
}
|
||||
|
||||
get displayValue(): string {
|
||||
return this.applyPipes(this.value);
|
||||
}
|
||||
}
|
@@ -20,7 +20,7 @@ import { DynamicComponentModel } from '../../common/services/dynamic-component-m
|
||||
import { CardViewBaseItemModel } from './card-view-baseitem.model';
|
||||
|
||||
export class CardViewMapItemModel extends CardViewBaseItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'map';
|
||||
type = 'map';
|
||||
value: Map<string, string>;
|
||||
|
||||
get displayValue() {
|
||||
|
@@ -23,7 +23,7 @@ import { Observable, of } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
|
||||
export class CardViewSelectItemModel<T> extends CardViewBaseItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'select';
|
||||
type = 'select';
|
||||
options$: Observable<CardViewSelectItemOption<T>[]>;
|
||||
displayNoneOption: boolean;
|
||||
|
||||
@@ -40,7 +40,8 @@ export class CardViewSelectItemModel<T> extends CardViewBaseItemModel implements
|
||||
switchMap((options) => {
|
||||
const option = options.find((o) => o.key === this.value?.toString());
|
||||
return of(option ? option.label : '');
|
||||
}));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
get displayValue() {
|
||||
|
@@ -21,8 +21,8 @@ import { CardViewBaseItemModel } from './card-view-baseitem.model';
|
||||
import { CardViewTextItemPipeProperty, CardViewTextItemProperties } from '../interfaces/card-view.interfaces';
|
||||
|
||||
export class CardViewTextItemModel extends CardViewBaseItemModel implements CardViewItem, DynamicComponentModel {
|
||||
type: string = 'text';
|
||||
inputType: string = 'text';
|
||||
type = 'text';
|
||||
inputType = 'text';
|
||||
multiline?: boolean;
|
||||
pipes?: CardViewTextItemPipeProperty[];
|
||||
clickCallBack?: any;
|
||||
|
@@ -21,6 +21,7 @@ export * from './card-view-dateitem.model';
|
||||
export * from './card-view-datetimeitem.model';
|
||||
export * from './card-view-floatitem.model';
|
||||
export * from './card-view-intitem.model';
|
||||
export * from './card-view-longitem.model';
|
||||
export * from './card-view-mapitem.model';
|
||||
export * from './card-view-textitem.model';
|
||||
export * from './card-view-keyvaluepairs.model';
|
||||
|
@@ -22,14 +22,17 @@ import { CardViewTextItemComponent } from '../components/card-view-textitem/card
|
||||
import { CardViewSelectItemComponent } from '../components/card-view-selectitem/card-view-selectitem.component';
|
||||
import { CardViewBoolItemComponent } from '../components/card-view-boolitem/card-view-boolitem.component';
|
||||
import { CardViewKeyValuePairsItemComponent } from '../components/card-view-keyvaluepairsitem/card-view-keyvaluepairsitem.component';
|
||||
import { DynamicComponentMapper, DynamicComponentResolveFunction, DynamicComponentResolver } from '../../common/services/dynamic-component-mapper.service';
|
||||
import {
|
||||
DynamicComponentMapper,
|
||||
DynamicComponentResolveFunction,
|
||||
DynamicComponentResolver
|
||||
} from '../../common/services/dynamic-component-mapper.service';
|
||||
import { CardViewArrayItemComponent } from '../components/card-view-arrayitem/card-view-arrayitem.component';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class CardItemTypeService extends DynamicComponentMapper {
|
||||
|
||||
protected defaultValue: Type<any> = CardViewTextItemComponent;
|
||||
|
||||
protected types: { [key: string]: DynamicComponentResolveFunction } = {
|
||||
@@ -37,6 +40,7 @@ export class CardItemTypeService extends DynamicComponentMapper {
|
||||
select: DynamicComponentResolver.fromType(CardViewSelectItemComponent),
|
||||
int: DynamicComponentResolver.fromType(CardViewTextItemComponent),
|
||||
float: DynamicComponentResolver.fromType(CardViewTextItemComponent),
|
||||
long: DynamicComponentResolver.fromType(CardViewTextItemComponent),
|
||||
date: DynamicComponentResolver.fromType(CardViewDateItemComponent),
|
||||
datetime: DynamicComponentResolver.fromType(CardViewDateItemComponent),
|
||||
bool: DynamicComponentResolver.fromType(CardViewBoolItemComponent),
|
||||
|
@@ -15,9 +15,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { CardViewItemLengthValidator } from './card-view-item-length.valiator';
|
||||
import { CardViewItemLengthValidator } from './card-view-item-length.validator';
|
||||
|
||||
describe('CardViewItemPositiveIntValidator', () => {
|
||||
describe('CardViewItemLengthValidator', () => {
|
||||
const validator = new CardViewItemLengthValidator(2, 3);
|
||||
|
||||
it('should validate empty value', () => {
|
||||
|
@@ -0,0 +1,39 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { CardViewItemValidator } from '../interfaces/card-view.interfaces';
|
||||
|
||||
export class CardViewItemLongValidator implements CardViewItemValidator {
|
||||
message = 'CORE.CARDVIEW.VALIDATORS.LONG_VALIDATION_ERROR';
|
||||
|
||||
isValid(value: number | number[] | ''): boolean {
|
||||
if (Array.isArray(value)) {
|
||||
return value.every(this.isLongNumber);
|
||||
}
|
||||
|
||||
return value === '' || (!isNaN(value) && this.isLongNumber(value) && this.isNotSpaceOnly(value));
|
||||
}
|
||||
|
||||
private isLongNumber(value: number): boolean {
|
||||
const longNumber = Number(value);
|
||||
return Math.trunc(longNumber) === longNumber && longNumber >= Number.MIN_SAFE_INTEGER && longNumber <= Number.MAX_SAFE_INTEGER;
|
||||
}
|
||||
|
||||
private isNotSpaceOnly(value: number): boolean {
|
||||
return String(value).trim() !== '';
|
||||
}
|
||||
}
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { CardViewItemMatchValidator } from './card-view-item-match.valiator';
|
||||
import { CardViewItemMatchValidator } from './card-view-item-match.validator';
|
||||
|
||||
describe('CardViewItemMatchValidator', () => {
|
||||
const validator = new CardViewItemMatchValidator('^[a-zA-Z]+$', undefined, true);
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { CardViewItemMinMaxValidator } from './card-view-item-minmax.valiator';
|
||||
import { CardViewItemMinMaxValidator } from './card-view-item-minmax.validator';
|
||||
|
||||
describe('CardViewItemMinMaxValidator', () => {
|
||||
const validator = new CardViewItemMinMaxValidator(1, 3);
|
||||
|
@@ -0,0 +1,34 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { CardViewItemValidator } from '../interfaces/card-view.interfaces';
|
||||
|
||||
export class CardViewItemPositiveLongValidator implements CardViewItemValidator {
|
||||
message = 'CORE.CARDVIEW.VALIDATORS.ONLY_POSITIVE_NUMBER';
|
||||
|
||||
isValid(value: number | number[] | ''): boolean {
|
||||
if (Array.isArray(value)) {
|
||||
return value.every(this.isPositiveNumber);
|
||||
}
|
||||
|
||||
return value === '' || (!isNaN(value) && this.isPositiveNumber(value));
|
||||
}
|
||||
|
||||
private isPositiveNumber(value: number): boolean {
|
||||
return Number(value) >= 0;
|
||||
}
|
||||
}
|
@@ -18,7 +18,9 @@
|
||||
export * from './card-view-item-int.validator';
|
||||
export * from './card-view-item-only-positive-int.validator';
|
||||
export * from './card-view-item-float.validator';
|
||||
export * from './card-view-item-match.valiator';
|
||||
export * from './card-view-item-minmax.valiator';
|
||||
export * from './card-view-item-length.valiator';
|
||||
export * from './card-view-item-long.validator';
|
||||
export * from './card-view-item-only-positive-long.validator';
|
||||
export * from './card-view-item-match.validator';
|
||||
export * from './card-view-item-minmax.validator';
|
||||
export * from './card-view-item-length.validator';
|
||||
export * from './validators.map';
|
||||
|
@@ -15,9 +15,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { CardViewItemMatchValidator, MatchValidatorParams } from './card-view-item-match.valiator';
|
||||
import { CardViewItemMinMaxValidator, MinMaxValidatorParams } from './card-view-item-minmax.valiator';
|
||||
import { CardViewItemLengthValidator, LengthValidatorParams } from './card-view-item-length.valiator';
|
||||
import { CardViewItemMatchValidator, MatchValidatorParams } from './card-view-item-match.validator';
|
||||
import { CardViewItemMinMaxValidator, MinMaxValidatorParams } from './card-view-item-minmax.validator';
|
||||
import { CardViewItemLengthValidator, LengthValidatorParams } from './card-view-item-length.validator';
|
||||
|
||||
const validators = {
|
||||
minmax: (parameters: MinMaxValidatorParams) => new CardViewItemMinMaxValidator(parameters.minValue, parameters.maxValue),
|
||||
|
@@ -196,6 +196,7 @@
|
||||
"VALIDATORS": {
|
||||
"FLOAT_VALIDATION_ERROR": "Use a number format",
|
||||
"INT_VALIDATION_ERROR": "Use an integer format",
|
||||
"LONG_VALIDATION_ERROR": "Use a long format",
|
||||
"LENGTH_VALIDATION_ERROR": "Value should be between {{ minLength }} and {{ maxLength }} in length",
|
||||
"MATCH_VALIDATION_ERROR": "Value doesn't match pattern: {{ expression }}",
|
||||
"MINMAX_VALIDATION_ERROR": "Value should be between {{ minValue }} and {{ maxValue }}",
|
||||
|
Reference in New Issue
Block a user