<commit_before>import { Prop, VueProp, InferType, PropType } from '../types'
import {
  isArray,
  isComplexType,
  isVueTypeDef,
  isFunction,
  toType,
  validateType,
  warn,
  indent,
} from '../utils'

export default function oneOfType<
  U extends VueProp<any> | Prop<any>,
  V = InferType<U>,
>(arr: U[]) {
  if (!isArray(arr)) {
    throw new TypeError(
      '[VueTypes error]: You must provide an array as argument',
    )
  }

  let hasCustomValidators = false

  let nativeChecks: Prop<V>[] = []

  for (let i = 0; i < arr.length; i += 1) {
    const type = arr[i]
    if (isComplexType<V>(type)) {
      if (
        isVueTypeDef<V>(type) &&
        type._vueTypes_name === 'oneOf' &&
        type.type
      ) {
        nativeChecks = nativeChecks.concat(type.type as PropType<V>)
        continue
      }
      if (isFunction(type.validator)) {
        hasCustomValidators = true
      }
      if (type.type !== true && type.type) {
        nativeChecks = nativeChecks.concat(type.type)
        continue
      }
    }
    nativeChecks.push(type as Prop<V>)
  }

  // filter duplicates
  nativeChecks = nativeChecks.filter((t, i) => nativeChecks.indexOf(t) === i)

  if (!hasCustomValidators) {
    // we got just native objects (ie: Array, Object)
    // delegate to Vue native prop check
    return toType<V>('oneOfType', {
      type: nativeChecks,
    })
  }

  return toType<V>('oneOfType', {
    type: nativeChecks,
    validator(value) {
      const err: string[] = []
      const valid = arr.some((type) => {
        const t =
          isVueTypeDef(type) && type._vueTypes_name === 'oneOf'
            ? type.type || null
            : type
        const res = validateType(t, value, true)
        if (typeof res === 'string') {
          err.push(res)
        }
        return res === true
      })
      if (!valid) {
        warn(
          `oneOfType - provided value does not match any of the ${
            err.length
          } passed-in validators:\n${indent(err.join('\n'))}`,
        )
      }

      return valid
    },
  })
}<commit_msg>Added casting support for oneOfType<commit_after>import { Prop, VueProp, InferType, PropType } from '../types'
import {
  isArray,
  isComplexType,
  isVueTypeDef,
  isFunction,
  toType,
  validateType,
  warn,
  indent,
} from '../utils'

export default function oneOfType<
  D extends InferType<U>,
  U extends VueProp<any> | Prop<any> = any,
  V = InferType<U> extends unknown ? U : InferType<U>,
>(arr: U[]) {
  if (!isArray(arr)) {
    throw new TypeError(
      '[VueTypes error]: You must provide an array as argument',
    )
  }

  let hasCustomValidators = false

  let nativeChecks: Prop<V>[] = []

  for (let i = 0; i < arr.length; i += 1) {
    const type = arr[i]
    if (isComplexType<V>(type)) {
      if (
        isVueTypeDef<V>(type) &&
        type._vueTypes_name === 'oneOf' &&
        type.type
      ) {
        nativeChecks = nativeChecks.concat(type.type as PropType<V>)
        continue
      }
      if (isFunction(type.validator)) {
        hasCustomValidators = true
      }
      if (type.type !== true && type.type) {
        nativeChecks = nativeChecks.concat(type.type)
        continue
      }
    }
    nativeChecks.push(type as Prop<V>)
  }

  // filter duplicates
  nativeChecks = nativeChecks.filter((t, i) => nativeChecks.indexOf(t) === i)

  if (!hasCustomValidators) {
    // we got just native objects (ie: Array, Object)
    // delegate to Vue native prop check
    return toType<D>('oneOfType', {
      type: nativeChecks as unknown as PropType<D>,
    })
  }

  return toType<D>('oneOfType', {
    type: nativeChecks as unknown as PropType<D>,
    validator(value) {
      const err: string[] = []
      const valid = arr.some((type) => {
        const t =
          isVueTypeDef(type) && type._vueTypes_name === 'oneOf'
            ? type.type || null
            : type
        const res = validateType(t, value, true)
        if (typeof res === 'string') {
          err.push(res)
        }
        return res === true
      })
      if (!valid) {
        warn(
          `oneOfType - provided value does not match any of the ${
            err.length
          } passed-in validators:\n${indent(err.join('\n'))}`,
        )
      }

      return valid
    },
  })
}
{'fraction_non_alphanumeric': 0.13622579892698858, 'fraction_numerical': 0.0009330534173081409, 'mean_word_length': 2.176296296296296, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, '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': '787373', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1389, 'n_words': 346}