Skip to content

Commit

Permalink
feat: validate on specific rows (#1984)
Browse files Browse the repository at this point in the history
* test: add test

* feat: validate on specific rows

* chore: apply code review
  • Loading branch information
jajugoguma authored Nov 16, 2023
1 parent 2e341d2 commit 2cd5e70
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
16 changes: 16 additions & 0 deletions packages/toast-ui.grid/cypress/integration/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/toast-ui.grid/src/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<number|string>} [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.<Object>} An array of error object
* @example
* // return value example
Expand Down Expand Up @@ -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);
}

/**
Expand Down
18 changes: 11 additions & 7 deletions packages/toast-ui.grid/src/query/validation.ts
Original file line number Diff line number Diff line change
@@ -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
);
Expand Down

0 comments on commit 2cd5e70

Please sign in to comment.