<commit_before>/* eslint-disable no-magic-numbers */
import Joi from "joi";

const JOI_OPTIONS = {
  abortEarly  : false,
  convert     : false,
  noDefaults  : true
};

/**
 * @return Joi Schema.
 */
function createSchema(): Object {
  const stringTrim = Joi.string().trim();
  const stringTrimNull = stringTrim.allow(null);

  const headersSchema = Joi.object({
    "content-disposition" : stringTrimNull,
    "content-encoding"    : stringTrimNull,
    "content-language"    : stringTrimNull,
    "content-length"      : Joi.number().allow(null),
    "content-md5"         : stringTrimNull,
    "content-type"        : stringTrimNull,
    expires             : stringTrimNull,
    location            : stringTrimNull.uri(),
    "cache-control"       : [stringTrimNull,
      Joi.object({
        maxAge      : [stringTrimNull, Joi.number().integer().positive()],
        cacheable   : stringTrimNull.valid("public", "private", "no-cache"),
        noTransform : Joi.boolean(),
        immutable   : Joi.boolean()
      })
    ]
  });

  /* const remoteSchema = Joi.object().pattern(/^[a-z0-9]+$/i, Joi.object({
    type: stringTrim.required(),
    basePath: stringTrim
  }).unknown());*/

  const remoteSchema = Joi.object({
    type : stringTrim.required(),
    basePath : stringTrim
  }).unknown();

  const schema = Joi.object({
    plugins : Joi.array().unique().items(
      // https://docs.npmjs.com/files/package.json
      stringTrim.lowercase().min(1).max(214).required()
    ),

    baseDir : stringTrim.required(),

    remote : Joi.alternatives().try(
      stringTrim,
      remoteSchema,
      Joi.object().pattern(/^[a-z0-9]+$/i, remoteSchema)
    ).required(),

    concurrency : Joi.number().integer().positive().min(1),

    readonly : [Joi.boolean(), Joi.object({
      cache : Joi.boolean(),
      remote : Joi.boolean()
    })],

    force : [Joi.boolean(), Joi.object({
      cache : Joi.boolean(),
      remote : Joi.boolean()
    })],

    cache : Joi.object({
      type      : stringTrim,
      location  : stringTrim,
      file      : stringTrim
    }).unknown(),

    each : Joi.array().items(Joi.object({
      match    : [stringTrim, Joi.array().items(stringTrim)],
      priority : Joi.number().allow(null),
      gzip     : Joi.boolean(),
      headers  : headersSchema,
      remote   : [stringTrimNull, Joi.object({
        id : stringTrim
      }).unknown()]
    })).required()
  }).with("plugins", "baseDir", "remote", "each");

  return schema;
}

/**
 * @static
 * @private
 */
function convertError(joiError: Object) {
  return joiError
    ? joiError.details.map(detail => `${detail.path}: ${detail.message}`)
    : undefined;
}

export default class OptionValidator {

  /* static async validate (options) {
    return await Joi.validateAsync(options, schema, JOI_OPTIONS);
  }*/

  /**
   * @param options Raw options.
   * @param plugins
   * @returns Error if validation failed.
   */
  static validateSync(options: Object, plugins: Object): ?Object {

    // 1. Get a brand new schema
    const schema = createSchema();

    // 2. Allow plugins to modify the schema
    plugins.validation.forEach(plugin => plugin.mutateJoiSchema(schema, Joi));

    // 3. Validation
    const error = Joi.validate(options, schema, JOI_OPTIONS).error;
    return convertError(error);
  }

}
<commit_msg>style: Fix indentation<commit_after>/* eslint-disable no-magic-numbers */
import Joi from "joi";

const JOI_OPTIONS = {
  abortEarly  : false,
  convert     : false,
  noDefaults  : true
};

/**
 * @return Joi Schema.
 */
function createSchema(): Object {
  const stringTrim = Joi.string().trim();
  const stringTrimNull = stringTrim.allow(null);

  const headersSchema = Joi.object({
    "content-disposition" : stringTrimNull,
    "content-encoding"    : stringTrimNull,
    "content-language"    : stringTrimNull,
    "content-length"      : Joi.number().allow(null),
    "content-md5"         : stringTrimNull,
    "content-type"        : stringTrimNull,
    expires               : stringTrimNull,
    location              : stringTrimNull.uri(),
    "cache-control"       : [stringTrimNull,
      Joi.object({
        maxAge      : [stringTrimNull, Joi.number().integer().positive()],
        cacheable   : stringTrimNull.valid("public", "private", "no-cache"),
        noTransform : Joi.boolean(),
        immutable   : Joi.boolean()
      })
    ]
  });

  /* const remoteSchema = Joi.object().pattern(/^[a-z0-9]+$/i, Joi.object({
    type: stringTrim.required(),
    basePath: stringTrim
  }).unknown());*/

  const remoteSchema = Joi.object({
    type      : stringTrim.required(),
    basePath  : stringTrim
  }).unknown();

  const schema = Joi.object({
    plugins : Joi.array().unique().items(
      // https://docs.npmjs.com/files/package.json
      stringTrim.lowercase().min(1).max(214).required()
    ),

    baseDir : stringTrim.required(),

    remote : Joi.alternatives().try(
      stringTrim,
      remoteSchema,
      Joi.object().pattern(/^[a-z0-9]+$/i, remoteSchema)
    ).required(),

    concurrency : Joi.number().integer().positive().min(1),

    readonly : [Joi.boolean(), Joi.object({
      cache   : Joi.boolean(),
      remote  : Joi.boolean()
    })],

    force : [Joi.boolean(), Joi.object({
      cache   : Joi.boolean(),
      remote  : Joi.boolean()
    })],

    cache : Joi.object({
      type      : stringTrim,
      location  : stringTrim,
      file      : stringTrim
    }).unknown(),

    each : Joi.array().items(Joi.object({
      match    : [stringTrim, Joi.array().items(stringTrim)],
      priority : Joi.number().allow(null),
      gzip     : Joi.boolean(),
      headers  : headersSchema,
      remote   : [stringTrimNull, Joi.object({
        id : stringTrim
      }).unknown()]
    })).required()
  }).with("plugins", "baseDir", "remote", "each");

  return schema;
}

/**
 * @static
 * @private
 */
function convertError(joiError: Object) {
  return joiError
    ? joiError.details.map(detail => `${detail.path}: ${detail.message}`)
    : undefined;
}

export default class OptionValidator {

  /* static async validate (options) {
    return await Joi.validateAsync(options, schema, JOI_OPTIONS);
  }*/

  /**
   * @param options Raw options.
   * @param plugins
   * @returns Error if validation failed.
   */
  static validateSync(options: Object, plugins: Object): ?Object {

    // 1. Get a brand new schema
    const schema = createSchema();

    // 2. Allow plugins to modify the schema
    plugins.validation.forEach(plugin => plugin.mutateJoiSchema(schema, Joi));

    // 3. Validation
    const error = Joi.validate(options, schema, JOI_OPTIONS).error;
    return convertError(error);
  }

}

{'fraction_non_alphanumeric': 0.15604001791847097, 'fraction_numerical': 0.0038823353740480814, 'mean_word_length': 2.6581103222282905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1357636', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2062, 'n_words': 424}