fix(882): fixes negative number submission for employees input (#1130)

* fix(882): fixes negative number submission for employees input

* formatting

* fix linting
This commit is contained in:
Srikar Samudrala 2023-08-10 10:55:57 +05:30 committed by GitHub
parent 510c466271
commit 4717f4cb90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 202 additions and 1 deletions

View File

@ -81,6 +81,7 @@ export const companyViewFields: ViewFieldDefinition<ViewFieldMetadata>[] = [
metadata: { metadata: {
type: 'number', type: 'number',
fieldName: 'employees', fieldName: 'employees',
isPositive: true,
}, },
isVisible: true, isVisible: true,
} satisfies ViewFieldDefinition<ViewFieldNumberMetadata>, } satisfies ViewFieldDefinition<ViewFieldNumberMetadata>,

View File

@ -39,6 +39,7 @@ export type ViewFieldDateMetadata = {
export type ViewFieldNumberMetadata = { export type ViewFieldNumberMetadata = {
type: 'number'; type: 'number';
fieldName: string; fieldName: string;
isPositive: boolean;
}; };
export type ViewFieldRelationMetadata = { export type ViewFieldRelationMetadata = {

View File

@ -7,6 +7,10 @@ import {
import { useCurrentRowEntityId } from '@/ui/table/hooks/useCurrentEntityId'; import { useCurrentRowEntityId } from '@/ui/table/hooks/useCurrentEntityId';
import { useUpdateEntityField } from '@/ui/table/hooks/useUpdateEntityField'; import { useUpdateEntityField } from '@/ui/table/hooks/useUpdateEntityField';
import { tableEntityFieldFamilySelector } from '@/ui/table/states/tableEntityFieldFamilySelector'; import { tableEntityFieldFamilySelector } from '@/ui/table/states/tableEntityFieldFamilySelector';
import {
canBeCastAsPositiveIntegerOrNull,
castAsPositiveIntegerOrNull,
} from '~/utils/cast-as-positive-integer-or-null';
import { TextCellEdit } from './TextCellEdit'; import { TextCellEdit } from './TextCellEdit';
@ -31,12 +35,27 @@ export function GenericEditableNumberCellEditMode({ viewField }: OwnProps) {
if (newText === fieldValue) return; if (newText === fieldValue) return;
try { try {
const numberValue = parseInt(newText); let numberValue = parseInt(newText);
if (isNaN(numberValue)) { if (isNaN(numberValue)) {
throw new Error('Not a number'); throw new Error('Not a number');
} }
if (viewField.metadata.isPositive) {
if (!canBeCastAsPositiveIntegerOrNull(newText)) {
return;
}
const valueCastedAsPositiveNumberOrNull =
castAsPositiveIntegerOrNull(newText);
if (valueCastedAsPositiveNumberOrNull === null) {
throw Error('Not a number');
}
numberValue = valueCastedAsPositiveNumberOrNull;
}
// TODO: find a way to store this better in DB // TODO: find a way to store this better in DB
if (numberValue > 2000000000) { if (numberValue > 2000000000) {
throw new Error('Number too big'); throw new Error('Number too big');

View File

@ -0,0 +1,116 @@
import {
canBeCastAsPositiveIntegerOrNull,
castAsPositiveIntegerOrNull,
} from '~/utils/cast-as-positive-integer-or-null';
describe('canBeCastAsPositiveIntegerOrNull', () => {
it(`should return true if null`, () => {
expect(canBeCastAsPositiveIntegerOrNull(null)).toBeTruthy();
});
it(`should return true if positive number`, () => {
expect(canBeCastAsPositiveIntegerOrNull(9)).toBeTruthy();
});
it(`should return false if negative number`, () => {
expect(canBeCastAsPositiveIntegerOrNull(-9)).toBeFalsy();
});
it(`should return false if zero`, () => {
expect(canBeCastAsPositiveIntegerOrNull(0)).toBeFalsy();
});
it(`should return false if string 0`, () => {
expect(canBeCastAsPositiveIntegerOrNull('0')).toBeFalsy();
});
it(`should return false if negative float`, () => {
expect(canBeCastAsPositiveIntegerOrNull(-1.22)).toBeFalsy();
});
it(`should return false if positive float`, () => {
expect(canBeCastAsPositiveIntegerOrNull(1.22)).toBeFalsy();
});
it(`should return false if positive float string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('0.9')).toBeFalsy();
});
it(`should return false if negative float string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('-0.9')).toBeFalsy();
});
it(`should return false if less than 1`, () => {
expect(canBeCastAsPositiveIntegerOrNull(0.22)).toBeFalsy();
});
it(`should return true if empty string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('')).toBeTruthy();
});
it(`should return true if integer string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('9')).toBeTruthy();
});
it(`should return false if undefined`, () => {
expect(canBeCastAsPositiveIntegerOrNull(undefined)).toBeFalsy();
});
it(`should return false if non numeric string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('9a')).toBeFalsy();
});
it(`should return false if non numeric string #2`, () => {
expect(canBeCastAsPositiveIntegerOrNull('a9a')).toBeFalsy();
});
});
describe('castAsPositiveIntegerOrNull', () => {
it(`should cast null to null`, () => {
expect(castAsPositiveIntegerOrNull(null)).toBe(null);
});
it(`should cast empty string to null`, () => {
expect(castAsPositiveIntegerOrNull('')).toBe(null);
});
it(`should cast an integer to positive integer`, () => {
expect(castAsPositiveIntegerOrNull(9)).toBe(9);
});
it(`should cast an integer string to positive integer`, () => {
expect(castAsPositiveIntegerOrNull('9')).toBe(9);
});
it(`should throw if trying to cast a 0 to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull(0)).toThrow(Error);
});
it(`should throw if trying to cast a string 0 to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull('0')).toThrow(Error);
});
it(`should throw if trying to cast a positive float string to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull('9.9')).toThrow(Error);
});
it(`should throw if trying to cast a negative float string to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull('-9.9')).toThrow(Error);
});
it(`should throw if trying to cast a positive float to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull(9.9)).toThrow(Error);
});
it(`should throw if trying to cast a negative float to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull(-9.9)).toThrow(Error);
});
it(`should throw if trying to cast a non numeric string to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull('9.9a')).toThrow(Error);
});
it(`should throw if trying to cast an undefined to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull(undefined)).toThrow(Error);
});
});

View File

@ -0,0 +1,64 @@
export function canBeCastAsPositiveIntegerOrNull(
probablePositiveNumberOrNull: string | undefined | number | null,
): probablePositiveNumberOrNull is number | null {
if (probablePositiveNumberOrNull === undefined) {
return false;
}
if (typeof probablePositiveNumberOrNull === 'number') {
return (
Number.isInteger(probablePositiveNumberOrNull) &&
Math.sign(probablePositiveNumberOrNull) === 1
);
}
if (probablePositiveNumberOrNull === null) {
return true;
}
if (probablePositiveNumberOrNull === '') {
return true;
}
if (typeof probablePositiveNumberOrNull === 'string') {
const stringAsNumber = +probablePositiveNumberOrNull;
if (isNaN(stringAsNumber)) {
return false;
}
if (Number.isInteger(stringAsNumber) && Math.sign(stringAsNumber) === 1) {
return true;
}
}
return false;
}
export function castAsPositiveIntegerOrNull(
probablePositiveNumberOrNull: string | undefined | number | null,
): number | null {
if (
canBeCastAsPositiveIntegerOrNull(probablePositiveNumberOrNull) === false
) {
throw new Error('Cannot cast to positive number or null');
}
if (probablePositiveNumberOrNull === null) {
return null;
}
if (probablePositiveNumberOrNull === '') {
return null;
}
if (typeof probablePositiveNumberOrNull === 'number') {
return probablePositiveNumberOrNull;
}
if (typeof probablePositiveNumberOrNull === 'string') {
return +probablePositiveNumberOrNull;
}
return null;
}