Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add props as extra argument to validators. #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/createValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ export default function createValidator(
return createValidator(curriedDefinition, newDefaultMessageCreator, ...args);
}

function validator(config, value, allValues) {
function validator(config, value, allValues, props) {
const message = getMessage(config, finalMessageCreator, ...args);
const valueValidator = curriedDefinition(message, ...args);

if (arguments.length <= 1) {
return markAsValueValidator(valueValidator);
}

return valueValidator(value, allValues);
return valueValidator(value, allValues, props);
}

validator.clone = clone;
Expand Down
6 changes: 3 additions & 3 deletions src/internal/createValidatorWithSingleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ export default function createValidatorWithSingleError(
validators: Array<Validator>,
sharedConfig: ComposeConfig,
): ConfiguredValidator {
return function composedValidator(value, allValues) {
return function composedValidator(value, allValues, props) {
for (let i = 0, l = validators.length; i < l; i++) {
const validator = validators[i];
let errorMessage;

if (isValueValidator(validator)) {
errorMessage = validator(value, allValues);
errorMessage = validator(value, allValues, props);
} else {
errorMessage = validator(sharedConfig, value, allValues);
errorMessage = validator(sharedConfig, value, allValues, props);
}

if (errorMessage) {
Expand Down
10 changes: 5 additions & 5 deletions src/internal/internalCombineValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ export default function internalCombineValidators(
return serializeValues(values) || {};
}

return function valuesValidator(values, allValues) {
return function valuesValidator(values, props) {
const serializedValues = finalSerializeValues(values);
const serializedAllValues = finalSerializeValues(allValues);
const serializedProps = finalSerializeValues(props);

const finalErrors = Object.keys(validators).reduce((errors, fieldName) => {
const parsedField = parseFieldName(fieldName);
const validator = validators[parsedField.fullName];
const value = serializedValues[parsedField.baseName];
const finalAllValues = atRoot ? serializedValues : serializedAllValues;
const finalAllValues = atRoot ? serializedValues : serializedProps;

const errorMessage = parsedField.isArray
? (value || []).map(fieldValue => validator(fieldValue, finalAllValues))
: validator(value, finalAllValues);
? (value || []).map(fieldValue => validator(fieldValue, finalAllValues, serializedProps))
: validator(value, finalAllValues, serializedProps);

if (errorMessage) {
errors[parsedField.baseName] = errorMessage;
Expand Down