<commit_before>import mongoose from 'mongoose';
import pick from 'lodash/pick';
import isEmail from 'validator/lib/isEmail';
import isMobilePhone from 'validator/lib/isMobilePhone';
import isUUID from 'validator/lib/isUUID';
import { isMobilePhoneLocales } from 'validator';

export class Email extends mongoose.SchemaType {
	cast (val) {
		const _val = String(val);

		if (!isEmail(_val)) {
			throw new Error(`${_val} is not a valid email address.`);
		}

		return _val;
	}
}

export class Phone extends mongoose.SchemaType {
	constructor (...args) {
		const _opts = args[1];
		const locales = isMobilePhoneLocales;

		if (_opts.locale) {
			let isValidLocale;
			if (Array.isArray(_opts.locale)) {
				isValidLocale = _opts.locale.every(locale => locales.includes(locale));
			} else {
				isValidLocale = locales.includes(_opts.locale);
			}

			if (!isValidLocale) {
				throw new Error(`${_opts.locale} is not a valid locale value for SchemaType Phone. [${isMobilePhoneLocales.join(',')}]`);
			}
		}

		super(...args);
	}
	cast (val) {
		const _val = String(val);
		const _validatorOpts = pick(this.options, ['strictMode']);

		if (!isMobilePhone(_val, this.options.locale || null, _validatorOpts)) {
			throw new Error(`${_val} is not a valid ${this.options.locale + ' ' || ''}phone number.`);
		}

		return _val;
	}
}

export class UUID extends mongoose.SchemaType {
	constructor (...args) {
		const _opts = args[1];
		const versions = [3, 4, 5];

		if (_opts.version && !versions.includes(_opts.version)) {
			throw new Error(`${_opts.version} is not a valid version value for SchemaType UUID. [${versions.join(',')}]`);
		}

		super(...args);
	}

	cast (val) {
		const _val = String(val);

		if (this.options.version) {
			if (!isUUID(_val, this.options.version)) {
				throw new Error(`${_val} is not a valid v${this.options.version} UUID.`);
			}
		} else {
			if (!isUUID(_val)) {
				throw new Error(`${_val} is not a valid UUID.`);<commit_msg>Add name arg to custom schema type constructors.<commit_after>import mongoose from 'mongoose';
import pick from 'lodash/pick';
import isEmail from 'validator/lib/isEmail';
import isMobilePhone from 'validator/lib/isMobilePhone';
import isUUID from 'validator/lib/isUUID';
import { isMobilePhoneLocales } from 'validator';

export class Email extends mongoose.SchemaType {
	constructor (...args) {
		super(...args, 'Email');
	}

	cast (val) {
		const _val = String(val);

		if (!isEmail(_val)) {
			throw new Error(`${_val} is not a valid email address.`);
		}

		return _val;
	}
}

export class Phone extends mongoose.SchemaType {
	constructor (...args) {
		const _opts = args[1];
		const locales = isMobilePhoneLocales;

		if (_opts.locale) {
			let isValidLocale;
			if (Array.isArray(_opts.locale)) {
				isValidLocale = _opts.locale.every(locale => locales.includes(locale));
			} else {
				isValidLocale = locales.includes(_opts.locale);
			}

			if (!isValidLocale) {
				throw new Error(`${_opts.locale} is not a valid locale value for SchemaType Phone. [${isMobilePhoneLocales.join(',')}]`);
			}
		}

		super(...args, 'Phone');
	}

	cast (val) {
		const _val = String(val);
		const _validatorOpts = pick(this.options, ['strictMode']);

		if (!isMobilePhone(_val, this.options.locale || null, _validatorOpts)) {
			throw new Error(`${_val} is not a valid ${this.options.locale + ' ' || ''}phone number.`);
		}

		return _val;
	}
}

export class UUID extends mongoose.SchemaType {
	constructor (...args) {
		const _opts = args[1];
		const versions = [3, 4, 5];

		if (_opts.version && !versions.includes(_opts.version)) {
			throw new Error(`${_opts.version} is not a valid version value for SchemaType UUID. [${versions.join(',')}]`);
		}

		super(...args, 'UUID');
	}

	cast (val) {
		const _val = String(val);

		if (this.options.version) {
			if (!isUUID(_val, this.options.version)) {
				throw new Error(`${_val} is not a valid v${this.options.version} UUID.`);
			}
		} else {
			if (!isUUID(_val)) {
				throw new Error(`${_val} is not a valid UUID.`);
{'fraction_non_alphanumeric': 0.15619567916563198, 'fraction_numerical': 0.0024832381425378696, 'mean_word_length': 3.800953516090584, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4702424', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1372, 'n_words': 364}