diff --git a/packages/toast-ui.grid/cypress/integration/validation.spec.ts b/packages/toast-ui.grid/cypress/integration/validation.spec.ts index 1b248f316..b1dbc524b 100644 --- a/packages/toast-ui.grid/cypress/integration/validation.spec.ts +++ b/packages/toast-ui.grid/cypress/integration/validation.spec.ts @@ -64,6 +64,22 @@ describe('should check the validation of cell - regExp', () => { }, ]); }); + it('get validation result of specific rows by validate API', () => { + cy.gridInstance() + .invoke('validate', [1]) + .should('eql', [ + { + errors: [ + { + columnName: 'name', + errorCode: ['REGEXP'], + errorInfo: [{ code: 'REGEXP', regExp: /[0-9]+:[0-9]/ }], + }, + ], + rowKey: 1, + }, + ]); + }); }); describe('should check the validation of cell - dataType: string', () => { diff --git a/packages/toast-ui.grid/src/grid.tsx b/packages/toast-ui.grid/src/grid.tsx index 647664fb9..57c97a7b8 100644 --- a/packages/toast-ui.grid/src/grid.tsx +++ b/packages/toast-ui.grid/src/grid.tsx @@ -1043,6 +1043,8 @@ export default class Grid implements TuiGrid { /** * Validate all data and returns the result. * Return value is an array which contains only rows which have invalid cell data. + * @param {Array} [rowKeys] - Array of rowKeys to validate. + * Validate only for the given rows, but validations that should be performed on all rows, such as unique, may not work correctly. * @returns {Array.} An array of error object * @example * // return value example @@ -1074,8 +1076,8 @@ export default class Grid implements TuiGrid { * } * ] */ - public validate(): InvalidRow[] { - return getInvalidRows(this.store); + public validate(rowKeys?: RowKey[]): InvalidRow[] { + return getInvalidRows(this.store, rowKeys); } /** diff --git a/packages/toast-ui.grid/src/query/validation.ts b/packages/toast-ui.grid/src/query/validation.ts index fcc82b3b9..791ad95bc 100644 --- a/packages/toast-ui.grid/src/query/validation.ts +++ b/packages/toast-ui.grid/src/query/validation.ts @@ -1,23 +1,27 @@ import { Store } from '@t/store'; -import { InvalidRow } from '@t/store/data'; +import { InvalidRow, RowKey } from '@t/store/data'; import { makeObservable } from '../dispatch/data'; import { isObservable } from '../helper/observable'; -import { createObservableData } from '../dispatch/lazyObservable'; - -export function getInvalidRows(store: Store) { - // @TODO: find more practical way to make observable - createObservableData(store, true); +export function getInvalidRows(store: Store, rowKeys?: RowKey[]) { const { data, column } = store; const invalidRows: InvalidRow[] = []; data.rawData.forEach((row, rowIndex) => { - if (!isObservable(row)) { + const needToValidateRow = !rowKeys || rowKeys.includes(row.rowKey); + + if (!isObservable(row) && needToValidateRow) { makeObservable(store, rowIndex, true); } }); data.viewData.forEach(({ rowKey, valueMap }) => { + const needToValidateRow = !rowKeys || rowKeys.includes(rowKey); + + if (!needToValidateRow) { + return; + } + const invalidColumns = column.validationColumns.filter( ({ name }) => !!valueMap[name].invalidStates.length );