Skip to content

Commit

Permalink
fix: remove current context from function
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa-yahia committed Nov 7, 2024
1 parent 4871302 commit 48bf0eb
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 24 deletions.
4 changes: 2 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-11-04T14:40:30.714Z\n"
"PO-Revision-Date: 2024-11-04T14:40:30.715Z\n"
"POT-Creation-Date: 2024-11-07T20:29:15.378Z\n"
"PO-Revision-Date: 2024-11-07T20:29:15.379Z\n"

msgid "Choose one or more dates..."
msgstr "Choose one or more dates..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class FormBuilder extends React.Component<Props> {
.reduce(async (passPromise, currentValidator) => {
const pass = await passPromise;
if (pass === true) {
let result = currentValidator.validator(value, validationContext,
let result = currentValidator.validator(value,
{ error: commitOptions?.error, errorCode: commitOptions?.errorCode });
if (result instanceof Promise) {
result = onIsValidatingInternal ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const validatorsForTypes = {
type: validatorTypes.TYPE_BASE,
},
{
validator: (value: string, allowFutureDate) => (allowFutureDate ? true : isValidNonFutureDate(value)),
validator: isValidNonFutureDate,
type: validatorTypes.TYPE_EXTENDED,
message: errorMessages.DATE_FUTURE_NOT_ALLOWED,
}],
Expand Down Expand Up @@ -212,14 +212,14 @@ function buildTypeValidators(metaData: DataElement | DateDataElement): ?Array<Va

validatorContainersForType = validatorContainersForType.map(validatorContainer => ({
...validatorContainer,
validator: (value: any, validationContext: ?Object, internalComponentError: ?Object) => {
validator: (value: any, internalComponentError: ?Object) => {
if (!value && value !== 0 && value !== false) {
return true;
}

const toValidateValue = isString(value) ? value.trim() : value;
// $FlowFixMe dataElementTypes flow error
return validatorContainer.validator(toValidateValue, metaData.allowFutureDate, internalComponentError);
return validatorContainer.validator(toValidateValue, internalComponentError);
},
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const preValidateDate = (value?: ?string, internalError?: ?Object) => {
return true;
}

return isValidDate(value, null, internalError);
return isValidDate(value, internalError);
};

export const getEventDateValidatorContainers = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function getValidationError(value: any, validatorContainers: ?Array<Valid
return false;
}

message = (result && result.errorMessage) || validatorContainer.message;
message = (result && result.errorMessage) || (result && result.message) || validatorContainer.message;
return true;
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { isValidAge as isValidAgeCore } from 'capture-core-utils/validators/form';

export function isValidAge(value: Object, currentContext?: Object, internalComponentError?: Object) {
export function isValidAge(value: Object, internalComponentError?: Object) {
return isValidAgeCore(value, internalComponentError);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { isValidDateTime as isValidDateTimeCore } from 'capture-core-utils/validators/form';

export function isValidDateTime(value: Object, currentContext?: Object, internalComponentError?: Object) {
export function isValidDateTime(value: Object, internalComponentError?: Object) {
return isValidDateTimeCore(value, internalComponentError);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { isValidDate as isValidDateCore } from 'capture-core-utils/validators/form';

export function isValidDate(value: string, currentContext?: Object, internalComponentError?: Object) {
export function isValidDate(value: string, internalComponentError?: Object) {
return isValidDateCore(value, internalComponentError);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
// @flow
import i18n from '@dhis2/d2-i18n';
import moment from 'moment';
import { parseDate } from '../../converters/date';

const CUSTOM_VALIDATION_MESSAGES = {
INVALID_DATE_MORE_THAN_MAX: i18n.t('A date in the future is not allowed'),
};

export const isValidNonFutureDate = (value: string) => {
const { isValid, momentDate } = parseDate(value);

if (!isValid) {
return isValid;
export const isValidNonFutureDate = (value: string, internalComponentError) => {
if (internalComponentError && internalComponentError?.errorCode === 'INVALID_DATE_MORE_THAN_MAX') {
return {
valid: !internalComponentError.error,
errorMessage: CUSTOM_VALIDATION_MESSAGES.INVALID_DATE_MORE_THAN_MAX,
};
}

return {
// $FlowFixMe -> if parseDate returns isValid true, there should always be a momentDate
valid: momentDate.isSameOrBefore(moment()),
errorMessage: CUSTOM_VALIDATION_MESSAGES.INVALID_DATE_MORE_THAN_MAX,
};
return true;
};

0 comments on commit 48bf0eb

Please sign in to comment.