verificator
Version:
Client and server-side validation JavaScript library
18 lines (17 loc) • 758 B
JavaScript
import requireParameterCount from './helpers/requireParameterCount';
import * as utils from '../utils';
const validate = (attribute, value, parameters, validator) => {
requireParameterCount(1, parameters, 'in_array');
const [path] = parameters;
const otherValues = [];
const explicitPath = path.split('*')[0].replace(/^\.|\.$/g, '') || null;
const attributeData = utils.flatten(validator.getValue(explicitPath));
const regex = new RegExp(`^${utils.escape(path).replace(/\\\*/g, '([^\.]+)')}`);
Object.keys(attributeData).forEach(key => {
if (regex.exec(`${explicitPath}.${key}`)) {
otherValues.push(attributeData[key]);
}
});
return otherValues.indexOf(value) > -1;
};
export default validate;