All files / lib/dynamic-form-validation dynamic-form-validation.service.ts

100% Statements 31/31
100% Branches 11/11
100% Functions 10/10
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73              1x       73x   73x   73x       12x 1x     11x 11x 5x     6x 6x 6x       4x 4x 2x   2x 1x   1x       5x       73x 73x 73x 73x 61x     12x 12x 13x                 12x 14x 13x      
import { Inject, Injectable, Optional } from '@angular/core';
import { DynamicFormLibraryService } from '../dynamic-form-library/dynamic-form-library.service';
import { DynamicFormValidationConfig, DynamicFormValidationConfigs,
  DYNAMIC_FORM_VALIDATION_CONFIGS } from './dynamic-form-validation-config';
import { DynamicFormValidationErrors } from './dynamic-form-validation-errors';
 
@Injectable()
export class DynamicFormValidationService {
  readonly validationConfig: DynamicFormValidationConfig;
 
  constructor(
    protected libraryService: DynamicFormLibraryService,
    @Optional() @Inject(DYNAMIC_FORM_VALIDATION_CONFIGS)
    private validationConfigs: DynamicFormValidationConfigs,
  ) {
    this.validationConfig = this.mergeValidationConfigs(this.validationConfigs);
  }
 
  getErrorMessage(errors: DynamicFormValidationErrors): string {
    if (!errors) {
      return null;
    }
 
    const keys = Object.keys(errors);
    if (!keys.length) {
      return this.getDefaultErrorMessage();
    }
 
    const errorKey = keys[0];
    const error = errors[errorKey];
    return error && error.message ? error.message : this.getErrorMessageFromConfig(errorKey, error);
  }
 
  private getErrorMessageFromConfig(errorKey: string, error: any): string {
    const message = this.validationConfig.messages[errorKey];
    if (typeof message === 'string') {
      return message;
    }
    if (typeof message === 'function') {
      return message(error);
    }
    return this.validationConfig.defaultMessage;
  }
 
  private getDefaultErrorMessage(): string {
    return this.validationConfig.defaultMessage;
  }
 
  private mergeValidationConfigs(configs: DynamicFormValidationConfigs): DynamicFormValidationConfig {
    const library = this.libraryService.library;
    const libraryName = library.name;
    const defaultConfig = { defaultMessage: undefined, messages: {}, libraryName };
    if (!configs || !configs.length) {
      return defaultConfig;
    }
 
    const libraryConfigs = this.getLibraryConfigs(configs);
    return libraryConfigs.reduce((result, config) => {
      return {
        ...result, ...config,
        messages: { ...result.messages, ...config.messages },
        libraryName,
      };
    }, defaultConfig);
  }
 
  private getLibraryConfigs(configs: DynamicFormValidationConfigs): DynamicFormValidationConfigs {
    return this.libraryService.libraryNamesReverse
      .map(name => configs.find(config => config.libraryName === name))
      .filter(config => !!config);
  }
}