<commit_before>'use strict';

const {AbstractValidate, BusinessCase} = require('business-error');
const {trim} = require('lodash');

const COLOR_MAXLENGTH = 20;
const NAME_MAXLENGTH = 100;

class ClientValidate extends AbstractValidate {
  constructor(app) {
    super();
    this.clientModel = app.domain.client.ClientModel;
  }

  onCreate(client) {
    return this.resolveValidationPromises(
      this.colorMustHaveMaximum20Characters(client),
      this.nameIsRequired(client),
      this.nameMustBeUnique(client),
      this.nameMustHaveMaximum100Characters(client)
    );
  }

  onUpdate(client) {
    return this.resolveValidationPromises(
      this.colorMustHaveMaximum20Characters(client),
      this.nameIsRequired(client),
      this.nameMustBeUnique(client),
      this.nameMustHaveMaximum100Characters(client)
    );
  }

  colorMustHaveMaximum20Characters({color}) {
    if (color && color.length > COLOR_MAXLENGTH) {
      const businessCase = new BusinessCase('client.color.maxlength', 'Color must have a maximum of 20 characters');

      return Promise.resolve(businessCase);
    }

    return Promise.resolve();
  }

  nameIsRequired({name}) {
    if (!trim(name)) {
      const businessCase = new BusinessCase('client.name.required', 'Name is required');

      return Promise.resolve(businessCase);
    }

    return Promise.resolve();
  }

  nameMustBeUnique({id, name}) {
    const quering = {
      attributes: ['id'],
      where: {name}
    };

    return this.clientModel.findOne(quering)
      .then(result => {
        if (result && result.id !== id) {
          return new BusinessCase('client.name.unique', 'Name must be unique');
        }

        return Promise.resolve();
      });
  }

  nameMustHaveMaximum100Characters({name}) {
    if (name && name.length > NAME_MAXLENGTH) {
      const businessCase = new BusinessCase('client.name.maxlength', 'Name must have a maximum of 100 characters');

      return Promise.resolve(businessCase);
    }

    return Promise.resolve();
  }
}

module.exports = ClientValidate;<commit_msg>fix code-style in api client validate<commit_after>'use strict';

const {AbstractValidate, BusinessCase} = require('business-error');
const {trim} = require('lodash');

const COLOR_MAXLENGTH = 20;
const NAME_MAXLENGTH = 100;

class ClientValidate extends AbstractValidate {
  constructor(app) {
    super();
    this.clientModel = app.domain.client.ClientModel;
  }

  onCreate(client) {
    return this.resolveValidationPromises(
      this.colorMustHaveMaximum20Characters(client),
      this.nameIsRequired(client),
      this.nameMustBeUnique(client),
      this.nameMustHaveMaximum100Characters(client)
    );
  }

  onUpdate(client) {
    return this.resolveValidationPromises(
      this.colorMustHaveMaximum20Characters(client),
      this.nameIsRequired(client),
      this.nameMustBeUnique(client),
      this.nameMustHaveMaximum100Characters(client)
    );
  }

  colorMustHaveMaximum20Characters({color}) {
    if (color && color.length > COLOR_MAXLENGTH) {
      const businessCase = new BusinessCase('client.color.maxlength', 'Color must have a maximum of 20 characters');

      return Promise.resolve(businessCase);
    }

    return Promise.resolve();
  }

  nameIsRequired({name}) {
    if (!trim(name)) {
      const businessCase = new BusinessCase('client.name.required', 'Name is required');

      return Promise.resolve(businessCase);
    }

    return Promise.resolve();
  }

  nameMustBeUnique({id, name}) {
    const quering = {
      attributes: ['id'],
      where: {name}
    };

    return this.clientModel.findOne(quering)
      .then(result => {
        if (result && result.id !== id) {
          return new BusinessCase('client.name.unique', 'Name must be unique');
        }

        return null;
      });
  }

  nameMustHaveMaximum100Characters({name}) {
    if (name && name.length > NAME_MAXLENGTH) {
      const businessCase = new BusinessCase('client.name.maxlength', 'Name must have a maximum of 100 characters');

      return Promise.resolve(businessCase);
    }

    return Promise.resolve();
  }
}

module.exports = ClientValidate;
{'fraction_non_alphanumeric': 0.11827178373159546, 'fraction_numerical': 0.012068549360366883, 'mean_word_length': 3.455913978494624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3003862', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1176, 'n_words': 242}