refactor(editor): add runtime type checks to database cell values (#10770)

This commit is contained in:
zzj3720 committed 2025-03-12 09:22:41 +00:00
1 parent fd3ce431fe
commit 01151ec18f
54 files changed
+775 -629

No files matched your search

@@ -16,6 +16,7 @@ import {
} from './utils/formatter.js';
export class NumberCell extends BaseCellRenderer<
number,
number,
NumberPropertyDataType
> {
@@ -2,25 +2,29 @@ import zod from 'zod';
import { t } from '../../core/logical/type-presets.js';
import { propertyType } from '../../core/property/property-config.js';
import type { NumberPropertyDataType } from './types.js';
import { NumberPropertySchema } from './types.js';
export const numberPropertyType = propertyType('number');
export const numberPropertyModelConfig = numberPropertyType.modelConfig<
number | undefined,
NumberPropertyDataType
>({
export const numberPropertyModelConfig = numberPropertyType.modelConfig({
name: 'Number',
valueSchema: zod.number().optional(),
type: () => t.number.instance(),
defaultData: () => ({ decimal: 0, format: 'number' }),
cellToString: ({ value }) => value?.toString() ?? '',
cellFromString: ({ value }) => {
const num = value ? Number(value) : NaN;
return {
value: isNaN(num) ? null : num,
};
propertyData: {
schema: NumberPropertySchema,
default: () => ({ decimal: 0, format: 'number' }) as const,
},
jsonValue: {
schema: zod.number().nullable(),
isEmpty: ({ value }) => value == null,
type: () => t.number.instance(),
},
rawValue: {
schema: zod.number().nullable(),
default: () => null,
toString: ({ value }) => value?.toString() ?? '',
fromString: ({ value }) => {
const num = value ? Number(value) : NaN;
return { value: isNaN(num) ? null : num };
},
toJson: ({ value }) => value ?? null,
fromJson: ({ value }) => (typeof value !== 'number' ? null : value),
},
cellToJson: ({ value }) => value ?? null,
cellFromJson: ({ value }) => (typeof value !== 'number' ? undefined : value),
isEmpty: ({ value }) => value == null,
});
@@ -1,6 +1,9 @@
import type { NumberFormat } from './utils/formatter.js';
import zod from 'zod';
export type NumberPropertyDataType = {
decimal?: number;
format?: NumberFormat;
};
import { NumberFormatSchema } from './utils/formatter.js';
export const NumberPropertySchema = zod.object({
decimal: zod.number().optional(),
format: NumberFormatSchema,
});
export type NumberPropertyDataType = zod.infer<typeof NumberPropertySchema>;
@@ -1,13 +1,16 @@
export type NumberFormat =
| 'number'
| 'numberWithCommas'
| 'percent'
| 'currencyYen'
| 'currencyINR'
| 'currencyCNY'
| 'currencyUSD'
| 'currencyEUR'
| 'currencyGBP';
import zod from 'zod';
export const NumberFormatSchema = zod.enum([
'number',
'numberWithCommas',
'percent',
'currencyYen',
'currencyINR',
'currencyCNY',
'currencyUSD',
'currencyEUR',
'currencyGBP',
]);
export type NumberFormat = zod.infer<typeof NumberFormatSchema>;
const currency = (currency: string): Intl.NumberFormatOptions => ({
style: 'currency',