Skip to content

Validation

カラム定義のオプションに基づいて自動的にバリデーションが行われます。

カラム型バリデーション項目
textrequired, maxLength, pattern
numberrequired, min, max
daterequired, minDate, maxDate
timerequired
listrequired
multiListrequired

バリデーションエラーのあるセルは赤くハイライトされ、ホバーするとエラーメッセージがツールチップで表示されます。警告レベルのセルはオレンジでハイライトされます。

validate オプションでカスタムバリデーション関数を指定できます。

import type { ValidationResult } from '@heynow-jp/react-spread-sheet-table'
function customValidate(
value: unknown,
row: MyRow,
columnKey: keyof MyRow,
): ValidationResult | null {
// null を返すとバリデーション OK
if (columnKey === 'score' && typeof value === 'number' && value > 80) {
return {
level: 'warn',
message: '高スコアです - 再確認してください',
}
}
return null
}
const table = useSpreadSheetTable<MyRow>({
columns,
initialData: data,
rowKey: 'id',
validate: customValidate,
})
type ValidationResult = {
level: 'error' | 'warn'
message: string
}
  • error - セルが赤くハイライト、isValid() が false を返す
  • warn - セルがオレンジでハイライト、isValid() には影響しない
const errors = table.getValidationErrors()
// => Array<{ rowIndex: number, columnKey: string, result: ValidationResult }>
const isValid = table.isValid()
// => boolean (error レベルのエラーがなければ true)
const table = useSpreadSheetTable<MyRow>({
columns,
initialData: data,
rowKey: 'id',
validate: customValidate,
onValidationError: (errors) => {
console.log('Validation errors:', errors)
},
})