CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
validator.js100 linesDownload Raw Back to helpers
1'use strict';2 3import {VERSION} from '../env/data.js';4import AxiosError from '../core/AxiosError.js';5 6const validators = {};7 8// eslint-disable-next-line func-names9['object', 'boolean', 'number', 'function', 'string', 'symbol'].forEach((type, i) => {10  validators[type] = function validator(thing) {11    return typeof thing === type || 'a' + (i < 1 ? 'n ' : ' ') + type;12  };13});14 15const deprecatedWarnings = {};16 17/**18 * Transitional option validator19 *20 * @param {function|boolean?} validator - set to false if the transitional option has been removed21 * @param {string?} version - deprecated version / removed since version22 * @param {string?} message - some message with additional info23 *24 * @returns {function}25 */26validators.transitional = function transitional(validator, version, message) {27  function formatMessage(opt, desc) {28    return '[Axios v' + VERSION + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');29  }30 31  // eslint-disable-next-line func-names32  return (value, opt, opts) => {33    if (validator === false) {34      throw new AxiosError(35        formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')),36        AxiosError.ERR_DEPRECATED37      );38    }39 40    if (version && !deprecatedWarnings[opt]) {41      deprecatedWarnings[opt] = true;42      // eslint-disable-next-line no-console43      console.warn(44        formatMessage(45          opt,46          ' has been deprecated since v' + version + ' and will be removed in the near future'47        )48      );49    }50 51    return validator ? validator(value, opt, opts) : true;52  };53};54 55validators.spelling = function spelling(correctSpelling) {56  return (value, opt) => {57    // eslint-disable-next-line no-console58    console.warn(`${opt} is likely a misspelling of ${correctSpelling}`);59    return true;60  }61};62 63/**64 * Assert object's properties type65 *66 * @param {object} options67 * @param {object} schema68 * @param {boolean?} allowUnknown69 *70 * @returns {object}71 */72 73function assertOptions(options, schema, allowUnknown) {74  if (typeof options !== 'object') {75    throw new AxiosError('options must be an object', AxiosError.ERR_BAD_OPTION_VALUE);76  }77  const keys = Object.keys(options);78  let i = keys.length;79  while (i-- > 0) {80    const opt = keys[i];81    const validator = schema[opt];82    if (validator) {83      const value = options[opt];84      const result = value === undefined || validator(value, opt, options);85      if (result !== true) {86        throw new AxiosError('option ' + opt + ' must be ' + result, AxiosError.ERR_BAD_OPTION_VALUE);87      }88      continue;89    }90    if (allowUnknown !== true) {91      throw new AxiosError('Unknown option ' + opt, AxiosError.ERR_BAD_OPTION);92    }93  }94}95 96export default {97  assertOptions,98  validators99};100