Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate on specific rows #1984

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines -8 to -9
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ผ์ „์— Tree ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด validation์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ์ด์Šˆ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด 14-18 ๋ผ์ธ์„ ์ถ”๊ฐ€ํ–ˆ๋Š”๋ฐ, ์‚ญ์ œํ•œ ์ฝ”๋“œ์™€ ์ค‘๋ณต๋œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.


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
Loading