Skip to content

Commit

Permalink
Merge pull request #63 from formio/FIO-8092
Browse files Browse the repository at this point in the history
FIO-8092: update isEmpty to isComponentDataEmpty and account for differing component data types
  • Loading branch information
travist authored Mar 22, 2024
2 parents ddba72c + 8a6d119 commit 98ff1de
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/process/validation/rules/validateTime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { RuleFn, RuleFnSync, TimeComponent, ValidationContext } from "types";
import { isEmpty } from "../util";
import { isComponentDataEmpty } from 'utils/formUtil';
import { FieldError, ValidatorError } from 'error';
import { dayjs } from 'utils/date';
import { ProcessorInfo } from "types/process/ProcessorInfo";
Expand All @@ -21,12 +21,12 @@ export const shouldValidate = (context: ValidationContext) => {
};

export const validateTimeSync: RuleFnSync = (context: ValidationContext) => {
const { component, value, config } = context;
const { component, data, path, value, config } = context;
if (!shouldValidate(context)) {
return null;
}
try {
if (!value || isEmpty(component, value)) return null;
if (!value || isComponentDataEmpty(component, data, path)) return null;
// Server side evaluations of validity should use the "dataFormat" vs the "format" which is used on the client.
const format = config?.server ?
((component as TimeComponent).dataFormat || 'HH:mm:ss') :
Expand Down
25 changes: 1 addition & 24 deletions src/process/validation/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FieldError } from 'error';
import { isArray, isEqual } from 'lodash';
import { Component, ValidationContext } from 'types';
import { Evaluator, unescapeHTML } from 'utils';
import { VALIDATION_ERRORS } from './i18n';
Expand Down Expand Up @@ -51,28 +50,6 @@ export function isObject(obj: any): obj is Object {
return typeof obj != null && (typeof obj === 'object' || typeof obj === 'function');
}

export function getEmptyValue(component: Component) {
switch (component.type) {
case 'textarea':
case 'textfield':
case 'time':
case 'datetime':
case 'day':
return '';
case 'datagrid':
case 'editgrid':
return [];

default:
return null;
}
}

export function isEmpty(component: Component, value: unknown) {
const isEmptyArray = (isArray(value) && value.length === 1) ? isEqual(value[0], getEmptyValue(component)) : false;
return value == null || (isArray(value) && value.length === 0) || isEmptyArray;
}

/**
* Interpolates @formio/core errors so that they are compatible with the renderer
* @param {FieldError[]} errors
Expand All @@ -95,7 +72,7 @@ export const interpolateErrors = (errors: FieldError[], lang: string = 'en') =>
paths.push(part);
}
});
return {
return {
message: unescapeHTML(Evaluator.interpolateString(toInterpolate, context)),
level: error.level,
path: paths,
Expand Down
5 changes: 5 additions & 0 deletions src/types/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export type ContainerComponent = NestedComponent & {
components: Component[];
};

export type HasChildComponents = BaseComponent & {
components: Component[];
}

export type AddressComponent = ContainerComponent & {
switchToManualModeLabel: string;
provider: string;
Expand Down Expand Up @@ -140,6 +144,7 @@ export type NumberComponent = BaseComponent & {

export type NestedArrayComponent = NestedComponent & {
disableAddingRemovingRows: boolean;
components: Component[];
};

export type DataGridComponent = NestedArrayComponent;
Expand Down
Loading

0 comments on commit 98ff1de

Please sign in to comment.