<commit_before>/**
 * Copyright 2017 Google Inc.
 *
 * 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 { fail } from './assert';
import { Code, FirestoreError } from './error';
import { AnyJs } from './misc';
import * as obj from './obj';

/**
 * Validates the invocation of functionName has the exact number of arguments.
 *
 * Forward the magic "arguments" variable as second parameter on which the
 * parameter validation is performed:
 * validateExactNumberOfArgs('myFunction', arguments, 2);
 */
export function validateExactNumberOfArgs(
  functionName: string,
  args: IArguments,
  numberOfArgs: number
): void {
  if (args.length !== numberOfArgs) {
    throw new FirestoreError(
      Code.INVALID_ARGUMENT,
      `Function ${functionName}() requires ` +
        formatPlural(numberOfArgs, 'argument') +
        ', but was called with ' +
        formatPlural(args.length, 'argument') +
        '.'
    );
  }
}

/**
 * Validates the invocation of functionName has at least the provided number of
 * arguments (but can have many more).
 *
 * Forward the magic "arguments" variable as second parameter on which the
 * parameter validation is performed:
 * validateAtLeastNumberOfArgs('myFunction', arguments, 2);
 */
export function validateAtLeastNumberOfArgs(
  functionName: string,
  args: IArguments,
  minNumberOfArgs: number
): void {
  if (args.length < minNumberOfArgs) {
    throw new FirestoreError(
      Code.INVALID_ARGUMENT,
      `Function ${functionName}() requires at least ` +
        formatPlural(minNumberOfArgs, 'argument') +
        ', but was called with ' +
        formatPlural(args.length, 'argument') +
        '.'
    );
  }
}

/**
 * Validates the invocation of functionName has number of arguments between
 * the values provided.
 *
 * Forward the magic "arguments" variable as second parameter on which the
 * parameter validation is performed:
 * validateBetweenNumberOfArgs('myFunction', arguments, 2, 3);
 */
export function validateBetweenNumberOfArgs(
  functionName: string,
  args: IArguments,
  minNumberOfArgs: number,
  maxNumberOfArgs: number
): void {
  if (args.length < minNumberOfArgs || args.length > maxNumberOfArgs) {
    throw new FirestoreError(
      Code.INVALID_ARGUMENT,
      `Function ${functionName}() requires between ${minNumberOfArgs} and ` +
        `${maxNumberOfArgs} arguments, but was called with ` +
        formatPlural(args.length, 'argument') +
        '.'
    );
  }
}

/**
 * Validates the provided argument is an array and has as least the expected
 * number of elements.
 */
export function validateNamedArrayAtLeastNumberOfElements<T>(
  functionName: string,
  value: T[],
  name: string,
  minNumberOfElements: number
): void {
  if (!(value instanceof Array) || value.length < minNumberOfElements) {
    throw new FirestoreError(
      Code.INVALID_ARGUMENT,
      `Function ${functionName}() requires its ${name} argument to be an ` +
        'array with at least ' +
        `${formatPlural(minNumberOfElements, 'element')}.`
    );
  }
}

/**
 * Validates the provided positional argument has the native JavaScript type
 * using typeof checks.
 */
export function validateArgType(
  functionName: string,
  type: string,
  position: number,
  argument: AnyJs
): void {
  validateType(functionName, type, `${ordinal(position)} argument`, argument);
}

/**
 * Validates the provided argument has the native JavaScript type using
 * typeof checks or is undefined.
 */
export function validateOptionalArgType(
  functionName: string,
  type: string,
  position: number,
  argument: AnyJs
): void {
  if (argument !== undefined) {
    validateArgType(functionName, type, position, argument);
  }
}

/**
 * Validates the provided named option has the native JavaScript type using
 * typeof checks.
 */
export function validateNamedType(
  functionName: string,
  type: string,
  optionName: string,
  argument: AnyJs
): void {
  validateType(functionName, type, `${optionName} option`, argument);
}

/**
 * Validates the provided named option has the native JavaScript type using
 * typeof checks or is undefined.
 */
export function validateNamedOptionalType(
  functionName: string,
  type: string,
  optionName: string,
  argument: AnyJs
): void {
  if (argument !== undefined) {
    validateNamedType(functionName, type, optionName, argument);
  }
}

/**
 * Validates that the provided named option equals one of the expected values.
 */
export function validateNamedPropertyEquals<T>(
  functionName: string,
  inputName: string,
  optionName: string,
  input: T,
  expected: T[]
): void {
  const expectedDescription: string[] = [];

  for (const val of expected) {
    if (val === input) {
      return;
    }
    expectedDescription.push(valueDescription(val));
  }

  const actualDescription = valueDescription(input);
  throw new FirestoreError(
    Code.INVALID_ARGUMENT,
    `Invalid value ${actualDescription} provided to function ${
      functionName
    }() for option "serverTimestamps". Acceptable values: ${expectedDescription.join(
      ', '
    )}`
  );
}

/**
 * Validates that the provided named option equals one of the expected values or
 * is undefined.
 */
export function validateNamedOptionalPropertyEquals<T>(
  functionName: string,
  inputName: string,
  optionName: string,
  input: T,
  expected: T[]
): void {
  if (input !== undefined) {
    validateNamedPropertyEquals(<commit_msg>Actually using the option name in validateNamedPropertyEquals (#480)<commit_after>/**
 * Copyright 2017 Google Inc.
 *
 * 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 { fail } from './assert';
import { Code, FirestoreError } from './error';
import { AnyJs } from './misc';
import * as obj from './obj';

/**
 * Validates the invocation of functionName has the exact number of arguments.
 *
 * Forward the magic "arguments" variable as second parameter on which the
 * parameter validation is performed:
 * validateExactNumberOfArgs('myFunction', arguments, 2);
 */
export function validateExactNumberOfArgs(
  functionName: string,
  args: IArguments,
  numberOfArgs: number
): void {
  if (args.length !== numberOfArgs) {
    throw new FirestoreError(
      Code.INVALID_ARGUMENT,
      `Function ${functionName}() requires ` +
        formatPlural(numberOfArgs, 'argument') +
        ', but was called with ' +
        formatPlural(args.length, 'argument') +
        '.'
    );
  }
}

/**
 * Validates the invocation of functionName has at least the provided number of
 * arguments (but can have many more).
 *
 * Forward the magic "arguments" variable as second parameter on which the
 * parameter validation is performed:
 * validateAtLeastNumberOfArgs('myFunction', arguments, 2);
 */
export function validateAtLeastNumberOfArgs(
  functionName: string,
  args: IArguments,
  minNumberOfArgs: number
): void {
  if (args.length < minNumberOfArgs) {
    throw new FirestoreError(
      Code.INVALID_ARGUMENT,
      `Function ${functionName}() requires at least ` +
        formatPlural(minNumberOfArgs, 'argument') +
        ', but was called with ' +
        formatPlural(args.length, 'argument') +
        '.'
    );
  }
}

/**
 * Validates the invocation of functionName has number of arguments between
 * the values provided.
 *
 * Forward the magic "arguments" variable as second parameter on which the
 * parameter validation is performed:
 * validateBetweenNumberOfArgs('myFunction', arguments, 2, 3);
 */
export function validateBetweenNumberOfArgs(
  functionName: string,
  args: IArguments,
  minNumberOfArgs: number,
  maxNumberOfArgs: number
): void {
  if (args.length < minNumberOfArgs || args.length > maxNumberOfArgs) {
    throw new FirestoreError(
      Code.INVALID_ARGUMENT,
      `Function ${functionName}() requires between ${minNumberOfArgs} and ` +
        `${maxNumberOfArgs} arguments, but was called with ` +
        formatPlural(args.length, 'argument') +
        '.'
    );
  }
}

/**
 * Validates the provided argument is an array and has as least the expected
 * number of elements.
 */
export function validateNamedArrayAtLeastNumberOfElements<T>(
  functionName: string,
  value: T[],
  name: string,
  minNumberOfElements: number
): void {
  if (!(value instanceof Array) || value.length < minNumberOfElements) {
    throw new FirestoreError(
      Code.INVALID_ARGUMENT,
      `Function ${functionName}() requires its ${name} argument to be an ` +
        'array with at least ' +
        `${formatPlural(minNumberOfElements, 'element')}.`
    );
  }
}

/**
 * Validates the provided positional argument has the native JavaScript type
 * using typeof checks.
 */
export function validateArgType(
  functionName: string,
  type: string,
  position: number,
  argument: AnyJs
): void {
  validateType(functionName, type, `${ordinal(position)} argument`, argument);
}

/**
 * Validates the provided argument has the native JavaScript type using
 * typeof checks or is undefined.
 */
export function validateOptionalArgType(
  functionName: string,
  type: string,
  position: number,
  argument: AnyJs
): void {
  if (argument !== undefined) {
    validateArgType(functionName, type, position, argument);
  }
}

/**
 * Validates the provided named option has the native JavaScript type using
 * typeof checks.
 */
export function validateNamedType(
  functionName: string,
  type: string,
  optionName: string,
  argument: AnyJs
): void {
  validateType(functionName, type, `${optionName} option`, argument);
}

/**
 * Validates the provided named option has the native JavaScript type using
 * typeof checks or is undefined.
 */
export function validateNamedOptionalType(
  functionName: string,
  type: string,
  optionName: string,
  argument: AnyJs
): void {
  if (argument !== undefined) {
    validateNamedType(functionName, type, optionName, argument);
  }
}

/**
 * Validates that the provided named option equals one of the expected values.
 */
export function validateNamedPropertyEquals<T>(
  functionName: string,
  inputName: string,
  optionName: string,
  input: T,
  expected: T[]
): void {
  const expectedDescription: string[] = [];

  for (const val of expected) {
    if (val === input) {
      return;
    }
    expectedDescription.push(valueDescription(val));
  }

  const actualDescription = valueDescription(input);
  throw new FirestoreError(
    Code.INVALID_ARGUMENT,
    `Invalid value ${actualDescription} provided to function ${
      functionName
    }() for option "${
      optionName
    }". Acceptable values: ${expectedDescription.join(', ')}`
  );
}

/**
 * Validates that the provided named option equals one of the expected values or
 * is undefined.
 */
export function validateNamedOptionalPropertyEquals<T>(
  functionName: string,
  inputName: string,
  optionName: string,
  input: T,
  expected: T[]
): void {
  if (input !== undefined) {
    validateNamedPropertyEquals(
{'fraction_non_alphanumeric': 0.10245625841184387, 'fraction_numerical': 0.0022711978465679676, 'mean_word_length': 3.790088638195004, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1208150', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3213, 'n_words': 1136}