From 668631c348f9711a6c3748a10acf83fecd2d549e Mon Sep 17 00:00:00 2001 From: "daeyeon.kim" Date: Wed, 15 Nov 2023 15:34:40 +0900 Subject: [PATCH 1/3] test: add test --- .../cypress/integration/validation.spec.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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', () => { From b532266148be92b36da76cb8d6c324944cda8edd Mon Sep 17 00:00:00 2001 From: "daeyeon.kim" Date: Wed, 15 Nov 2023 15:36:50 +0900 Subject: [PATCH 2/3] feat: validate on specific rows --- packages/toast-ui.grid/src/grid.tsx | 6 ++- .../toast-ui.grid/src/query/validation.ts | 44 +++++++++---------- 2 files changed, 25 insertions(+), 25 deletions(-) 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..847bb3009 100644 --- a/packages/toast-ui.grid/src/query/validation.ts +++ b/packages/toast-ui.grid/src/query/validation.ts @@ -1,39 +1,37 @@ 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)) { + if (!isObservable(row) && (!rowKeys || rowKeys.includes(row.rowKey))) { makeObservable(store, rowIndex, true); } }); data.viewData.forEach(({ rowKey, valueMap }) => { - const invalidColumns = column.validationColumns.filter( - ({ name }) => !!valueMap[name].invalidStates.length - ); - - if (invalidColumns.length) { - const errors = invalidColumns.map(({ name }) => { - const { invalidStates } = valueMap[name]; - - return { - columnName: name, - errorInfo: invalidStates, - errorCode: invalidStates.map(({ code }) => code), - }; - }); - - invalidRows.push({ rowKey, errors }); + if (!rowKeys || rowKeys?.includes(rowKey)) { + const invalidColumns = column.validationColumns.filter( + ({ name }) => !!valueMap[name].invalidStates.length + ); + + if (invalidColumns.length) { + const errors = invalidColumns.map(({ name }) => { + const { invalidStates } = valueMap[name]; + + return { + columnName: name, + errorInfo: invalidStates, + errorCode: invalidStates.map(({ code }) => code), + }; + }); + + invalidRows.push({ rowKey, errors }); + } } }); From 2699092c937ad8a0e1448bb367ef05bff3627f21 Mon Sep 17 00:00:00 2001 From: "daeyeon.kim" Date: Thu, 16 Nov 2023 10:21:21 +0900 Subject: [PATCH 3/3] chore: apply code review --- .../toast-ui.grid/src/query/validation.ts | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/toast-ui.grid/src/query/validation.ts b/packages/toast-ui.grid/src/query/validation.ts index 847bb3009..791ad95bc 100644 --- a/packages/toast-ui.grid/src/query/validation.ts +++ b/packages/toast-ui.grid/src/query/validation.ts @@ -8,30 +8,36 @@ export function getInvalidRows(store: Store, rowKeys?: RowKey[]) { const invalidRows: InvalidRow[] = []; data.rawData.forEach((row, rowIndex) => { - if (!isObservable(row) && (!rowKeys || rowKeys.includes(row.rowKey))) { + const needToValidateRow = !rowKeys || rowKeys.includes(row.rowKey); + + if (!isObservable(row) && needToValidateRow) { makeObservable(store, rowIndex, true); } }); data.viewData.forEach(({ rowKey, valueMap }) => { - if (!rowKeys || rowKeys?.includes(rowKey)) { - const invalidColumns = column.validationColumns.filter( - ({ name }) => !!valueMap[name].invalidStates.length - ); - - if (invalidColumns.length) { - const errors = invalidColumns.map(({ name }) => { - const { invalidStates } = valueMap[name]; - - return { - columnName: name, - errorInfo: invalidStates, - errorCode: invalidStates.map(({ code }) => code), - }; - }); - - invalidRows.push({ rowKey, errors }); - } + const needToValidateRow = !rowKeys || rowKeys.includes(rowKey); + + if (!needToValidateRow) { + return; + } + + const invalidColumns = column.validationColumns.filter( + ({ name }) => !!valueMap[name].invalidStates.length + ); + + if (invalidColumns.length) { + const errors = invalidColumns.map(({ name }) => { + const { invalidStates } = valueMap[name]; + + return { + columnName: name, + errorInfo: invalidStates, + errorCode: invalidStates.map(({ code }) => code), + }; + }); + + invalidRows.push({ rowKey, errors }); } });