[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:
Cilibiu Bogdan
2020-07-24 18:58:07 +03:00
committed by GitHub
parent 154ca07aad
commit aa3d890342
15 changed files with 223 additions and 12 deletions

View File

@@ -0,0 +1,34 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* 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 interface LengthValidatorParams {
minLength: number;
maxLength: number;
}
export class CardViewItemLengthValidator implements CardViewItemValidator {
message = 'CORE.CARDVIEW.VALIDATORS.LENGTH_VALIDATION_ERROR';
constructor(private minLength: number, private maxLength: number) {}
isValid(value: string = ''): boolean {
const stringLength = value.length;
return stringLength >= this.minLength && stringLength <= this.maxLength;
}
}

View File

@@ -0,0 +1,34 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* 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 interface MatchValidatorParams {
expression: string;
flags?: string;
}
export class CardViewItemMatchValidator implements CardViewItemValidator {
message = 'CORE.CARDVIEW.VALIDATORS.MATCH_VALIDATION_ERROR';
constructor(private expression: string, private flags?: string) {}
isValid(value: string): boolean {
const regex = new RegExp(this.expression, this.flags);
return value === '' || regex.test(value);
}
}

View File

@@ -0,0 +1,37 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* 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';
import { CardViewItemIntValidator } from './card-view-item-int.validator';
export interface MinMaxValidatorParams {
minValue: number;
maxValue: number;
}
export class CardViewItemMinMaxValidator implements CardViewItemValidator {
message = 'CORE.CARDVIEW.VALIDATORS.MINMAX_VALIDATION_ERROR';
private intValidator: CardViewItemIntValidator;
constructor(private minValue: number, private maxValue: number) {
this.intValidator = new CardViewItemIntValidator();
}
isValid(value: number): boolean {
return this.intValidator.isValid(value) && (value >= this.minValue && value <= this.maxValue);
}
}

View File

@@ -17,3 +17,7 @@
export * from './card-view-item-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 './validators.map';

View File

@@ -0,0 +1,27 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* 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 { 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';
const validators = {
minmax: (parameters: MinMaxValidatorParams) => new CardViewItemMinMaxValidator(parameters.minValue, parameters.maxValue),
regex: (parameters: MatchValidatorParams) => new CardViewItemMatchValidator(parameters.expression),
length: (parameters: LengthValidatorParams) => new CardViewItemLengthValidator(parameters.minLength, parameters.maxLength)
};
export default validators;