import Bourne from '@hapi/bourne';
import Joi from 'joi';

const validator = Joi.extend((joi) => ({
	type: 'array',
	base: Joi.array(),
	coerce: {
		from: 'string',
		method(value, helpers) {
			if (typeof value !== 'string' || (value[0] !== '[' && !/^\s*\[/.test(value))) {
				return { value };
			}

			try {
				return { value: Bourne.parse(value) };
			} catch (ignoreErr) {
				return { value };
			}
		}
	}
}));

export default validator;
