Skip to content

Commit

Permalink
refactor: cleanup validation stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
soheilous committed Apr 28, 2024
1 parent 734e69f commit ec33964
Show file tree
Hide file tree
Showing 42 changed files with 188 additions and 633 deletions.
4 changes: 3 additions & 1 deletion apps/server/nodemon.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/nodemon.json",
"ignore": ["./testSrc/**", "./thunder-tests/**"],
"exec": "node --no-warnings --loader ./loader.js",
"ext": "ts,json"
"ext": "ts,json",
"watch": ["src", "../../packages"]
}
13 changes: 13 additions & 0 deletions apps/server/src/classes/RouteBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NativeError } from "@repo/error-store";
import { models } from "@repo/validator";
import FastestValidator, { ValidationSchema } from "fastest-validator";

import { Route } from "~/types";
Expand Down Expand Up @@ -43,3 +44,15 @@ export abstract class RouteBuilder {
};
}
}

const validator = compiler.compile({
phoneNumber: models.validation.phoneNumber,
lastName: models.validation.lastName,
// firstName: models.validation.firstName,
// countryCode: models.validation.countryCode,
// countryName: models.validation.countryName,
});

const _result = validator({
phoneNumber: "123123123",
});
3 changes: 0 additions & 3 deletions apps/web/src/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ export * from "./Storage";
export * from "./StuffStore";
export * from "./UserUtils";
export * from "./index";
export * from "./validator/OnChangeValidator";
export * from "./validator/SubmitValidator";
export * from "./validator/Validator";
export * from "./websocket/EventHandler";
export * from "./websocket/IOMutator";
export * from "./websocket/SocketEmitterStore";
Expand Down
76 changes: 0 additions & 76 deletions apps/web/src/classes/validator/OnChangeValidator.ts

This file was deleted.

28 changes: 0 additions & 28 deletions apps/web/src/classes/validator/SubmitValidator.ts

This file was deleted.

72 changes: 0 additions & 72 deletions apps/web/src/classes/validator/Validator.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import Autocomplete from "@mui/material/Autocomplete";
import type { CountryItem } from "@repo/type-store";
import { countries } from "@repo/vars";

import { OnChangeValidatorFn, SelectedCountry, VoidWithArg } from "~/types";
import { utils } from "~/utils";
import { SelectedCountry, VoidWithArg } from "~/types";

import Option from "./Option";
import SelectorInput from "./SelectorInput";
Expand All @@ -14,36 +13,12 @@ export type SelectCountryOnChange = VoidWithArg<SelectedCountry>;
interface Props {
countryCode: string;
countryName: string;
countryNameOnChange: OnChangeValidatorFn;
onSelectChange: SelectCountryOnChange;
}

const CountrySelector: React.FC<Props> = ({
countryCode,
countryName,
countryNameOnChange,
onSelectChange,
}) => {
const CountrySelector: React.FC<Props> = ({ countryCode, countryName }) => {
const getOptionLabel = (option: CountryItem) => option.countryName;

const handleCountryNameOnChange = utils.createOnChangeValidator(
"countryName",
(value: string) => {
countryNameOnChange(value, {
target: {
value,
name: "countryName",
},
});
}
);

const handleSelectCountryOnChange = (
_e: React.SyntheticEvent,
newValue: SelectedCountry
) => {
onSelectChange(newValue);
};
const handleSelectCountryOnChange = (_e: React.SyntheticEvent) => {};

const renderOption = (props: ListItemProps, option: CountryItem) => (
<Option key={option.countryName} option={option} props={props} />
Expand All @@ -63,7 +38,7 @@ const CountrySelector: React.FC<Props> = ({
renderOption={renderOption}
value={selectedCountry}
onChange={handleSelectCountryOnChange}
onInputChange={handleCountryNameOnChange}
// onInputChange={handleCountryNameOnChange}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { stuffStore } from "~/classes/StuffStore";
import { BaseComponent } from "~/components/Base";
import { OnChangeValidatorFn } from "~/types";
import { utils } from "~/utils";
import { CommonOnChange } from "~/types";

interface Props {
onChange: OnChangeValidatorFn;
onChange: CommonOnChange;
value: string;
}

const Bio: React.FC<Props> = ({ onChange, value }) => {
const handleChange = utils.createOnChangeValidator("bio", onChange);

return (
<BaseComponent.Input.Text
InputProps={{
Expand All @@ -25,7 +22,7 @@ const Bio: React.FC<Props> = ({ onChange, value }) => {
multiline
name="bio"
value={value}
onChange={handleChange}
onChange={onChange}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { countries } from "@repo/vars";

import { BaseComponent } from "~/components/Base";
import { OnChangeValidatorFn, SelectedCountry } from "~/types";
import { CommonOnChange } from "~/types";

import CountrySelector from "../Select/CountrySelector";
import CountryCode from "./CountryCode";
Expand All @@ -11,7 +9,7 @@ interface Props {
countryCode: string;
countryName: string;
phoneNumber: string;
onChange: OnChangeValidatorFn;
onChange: CommonOnChange;
}

const Cellphone: React.FC<Props> = ({
Expand All @@ -20,51 +18,13 @@ const Cellphone: React.FC<Props> = ({
onChange,
phoneNumber,
}) => {
const handleCountryNameOnChange: OnChangeValidatorFn = (countryName, e) => {
onChange(countryName, e);
};

const handleSelectedCountryOnChange = (value: SelectedCountry) => {
const { countryCode = "", countryName = "" } = value || {};

onChange(countryName, {
target: {
name: "countryName",
value: countryName,
},
});

onChange(countryCode, {
target: {
name: "countryCode",
value: countryCode,
},
});
};

const handleCountryCodeOnChange: OnChangeValidatorFn = (value, e) => {
const country = countries.find((i) => i.countryCode === value);

onChange(value, e);
onChange(country?.countryName || "", e);
};

const handlePhoneNumberOnChange: OnChangeValidatorFn = (value, e) => {
onChange(value, e);
};

return (
<>
<CountrySelector
countryCode={countryCode}
countryName={countryName}
countryNameOnChange={handleCountryNameOnChange}
onSelectChange={handleSelectedCountryOnChange}
/>
<CountrySelector countryCode={countryCode} countryName={countryName} />

<BaseComponent.Box.Flex jc="space-between" style={{ width: "100%" }}>
<CountryCode value={countryCode} onChange={handleCountryCodeOnChange} />
<PhoneNumber value={phoneNumber} onChange={handlePhoneNumberOnChange} />
<CountryCode value={countryCode} onChange={onChange} />
<PhoneNumber value={phoneNumber} onChange={onChange} />
</BaseComponent.Box.Flex>
</>
);
Expand Down
Loading

0 comments on commit ec33964

Please sign in to comment.