Skip to content

Commit

Permalink
fix(provider/cf): make validators more robust (#6047)
Browse files Browse the repository at this point in the history
- improve error messages for instance size validation
- trim JSON when checking input size

Co-Authored-By: Jammy Louie <jlouie@pivotal.io>
  • Loading branch information
Jammy Louie authored and jkschneider committed Dec 5, 2018
1 parent 96b8d0e commit cd62a34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ export class CfInstanceSizeFieldValidator implements IStageOrTriggerValidator {

protected validationMessage(validationConfig: IInstanceFieldSizeValidationConfig, pipeline: IPipeline): string {
const fieldLabel: string = this.printableFieldLabel(validationConfig);
const min: any = has(validationConfig, 'min') ? get(validationConfig, 'min') : 'NA';
const max: any = has(validationConfig, 'max') ? get(validationConfig, 'max') : 'NA';
return (
validationConfig.message ||
`<strong>${fieldLabel}</strong> should be between ${min} and ${max} in ${pipeline.name}`
);
const hasMin = has(validationConfig, 'min');
const min: any = hasMin ? get(validationConfig, 'min') : 'NA';
const hasMax = has(validationConfig, 'max');
const max: any = hasMax ? get(validationConfig, 'max') : 'NA';
let message = ``;
if (hasMin) {
if (hasMax) {
message = `<strong>${fieldLabel}</strong> should be from ${min} to ${max} in ${pipeline.name}.`;
} else {
message = `<strong>${fieldLabel}</strong> should be at least ${min} in ${pipeline.name}.`;
}
} else if (hasMax) {
message = `<strong>${fieldLabel}</strong> should be no more than ${max} in ${pipeline.name}.`;
}
return validationConfig.message || message;
}

protected printableFieldLabel(config: IInstanceFieldSizeValidationConfig): string {
Expand All @@ -56,8 +65,7 @@ export class CfInstanceSizeFieldValidator implements IStageOrTriggerValidator {
const max: number = get(config, 'max');
const min: number = get(config, 'min');

const result: any = fieldExists && ((!hasMax || (hasMax && field <= max)) && (!hasMin || (hasMin && field >= min)));
return result;
return fieldExists && ((!hasMax || (hasMax && field <= max)) && (!hasMin || (hasMin && field >= min)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,40 @@ export interface IServiceParameterJsonValidationConfig extends IValidatorConfig

export class ServiceParameterJsonFieldValidator implements IStageOrTriggerValidator {
public validate(
_pipeline: IPipeline,
pipeline: IPipeline,
stage: IStage | ITrigger,
validationConfig: IServiceParameterJsonValidationConfig,
): string {
if (!this.fieldIsValid(stage, validationConfig)) {
return this.validationMessage(validationConfig);
if (!this.passesValidation(stage, validationConfig)) {
return this.validationMessage(validationConfig, pipeline);
}
return null;
}

private validationMessage(validationConfig: IServiceParameterJsonValidationConfig): string {
protected passesValidation(
stage: IStage | ITrigger,
validationConfig: IServiceParameterJsonValidationConfig,
): boolean {
return this.fieldIsValid(stage, validationConfig);
}

protected validationMessage(validationConfig: IServiceParameterJsonValidationConfig, pipeline: IPipeline): string {
const fieldLabel: string = this.printableFieldLabel(validationConfig);
return validationConfig.message || `<strong>${fieldLabel}</strong> should be a valid JSON string.`;
return (
validationConfig.message || `<strong>${fieldLabel}</strong> should be a valid JSON string in ${pipeline.name}`
);
}

private printableFieldLabel(config: IServiceParameterJsonValidationConfig): string {
protected printableFieldLabel(config: IServiceParameterJsonValidationConfig): string {
const fieldLabel: string = config.fieldLabel || config.fieldName;
return upperFirst(fieldLabel);
}

private fieldIsValid(stage: IStage | ITrigger, config: IServiceParameterJsonValidationConfig): boolean {
protected fieldIsValid(stage: IStage | ITrigger, config: IServiceParameterJsonValidationConfig): boolean {
const fieldExists = has(stage, config.fieldName);
const field: any = get(stage, config.fieldName);

if (!fieldExists || !field) {
if (!fieldExists || !field.trim()) {
return true;
}

Expand Down

0 comments on commit cd62a34

Please sign in to comment.