diff --git a/apps/server/nodemon.json b/apps/server/nodemon.json index c98d2b6..f1be19e 100644 --- a/apps/server/nodemon.json +++ b/apps/server/nodemon.json @@ -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"] } diff --git a/apps/server/src/classes/RouteBuilder.ts b/apps/server/src/classes/RouteBuilder.ts index 8c3f2f2..f0a0105 100644 --- a/apps/server/src/classes/RouteBuilder.ts +++ b/apps/server/src/classes/RouteBuilder.ts @@ -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"; @@ -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", +}); diff --git a/apps/web/src/classes/index.ts b/apps/web/src/classes/index.ts index 4b2c422..30ac46b 100644 --- a/apps/web/src/classes/index.ts +++ b/apps/web/src/classes/index.ts @@ -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"; diff --git a/apps/web/src/classes/validator/OnChangeValidator.ts b/apps/web/src/classes/validator/OnChangeValidator.ts deleted file mode 100644 index 0d8fe01..0000000 --- a/apps/web/src/classes/validator/OnChangeValidator.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { - CommonChangeEvent, - ErrorTypeItem, - Field, - ValidationModel, - ValidationResult, - ValidatorType, -} from "~/types"; - -import { Validator } from "./Validator"; - -export class OnChangeValidator extends Validator { - changeEvent: CommonChangeEvent | React.SyntheticEvent; - - checkValue(e: CommonChangeEvent | React.SyntheticEvent, value?: string) { - this.value = - value ?? - { - value: "", - ...e.target, - }.value; - - this.changeEvent = e; - - const validationResult = this.compiledValidator( - this.value - ) as ValidationResult; - - if (Array.isArray(validationResult)) { - const extraIgnoreTypes: ErrorTypeItem[] = []; - if (this.value === "") extraIgnoreTypes.push("stringNumeric"); - if ( - validationResult.some( - (i) => (i.type as ErrorTypeItem) === "stringLength" - ) && - this.value.length < this.model.length - ) { - extraIgnoreTypes.push("stringLength"); - } - - const filteredValidationResult = validationResult.filter( - (errorItem) => - ![ - ...this.ignoredErrorTypesForInputValidator, - ...extraIgnoreTypes, - ].includes(errorItem.type as ErrorTypeItem) - ); - - this.validationResult = filteredValidationResult; - } else { - this.validationResult = []; - } - - return this; - } - - executeIfNoError( - cb: (value: any, e: CommonChangeEvent | React.SyntheticEvent) => void - ) { - if ( - Array.isArray(this.validationResult) && - this.validationResult.length === 0 - ) { - cb(this.value, this.changeEvent); - } - } -} - -export const onChangeValidator = - ( - fieldName: Field, - compiledValidator: ValidatorType, - model: ValidationModel - ) => - () => - new OnChangeValidator(fieldName, compiledValidator, model); diff --git a/apps/web/src/classes/validator/SubmitValidator.ts b/apps/web/src/classes/validator/SubmitValidator.ts deleted file mode 100644 index b50d731..0000000 --- a/apps/web/src/classes/validator/SubmitValidator.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { - Field, - ValidationModel, - ValidationResult, - ValidatorType, -} from "~/types"; - -import { Validator } from "./Validator"; - -export class SubmitValidator extends Validator { - checkValue(value: any) { - this.validationResult = this.compiledValidator(value) as ValidationResult; - - if (this.validationResult === true) this.validationResult = []; - else this.hasError = true; - - return this; - } -} - -export const submitValidator = - ( - fieldName: Field, - compiledValidator: ValidatorType, - model: ValidationModel - ) => - () => - new SubmitValidator(fieldName, compiledValidator, model); diff --git a/apps/web/src/classes/validator/Validator.ts b/apps/web/src/classes/validator/Validator.ts deleted file mode 100644 index 2440ee7..0000000 --- a/apps/web/src/classes/validator/Validator.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { - ErrorChecker, - ErrorTypeItem, - Field, - NativeError, - ValidationModel, - ValidationResult, - ValidatorType, -} from "~/types"; -import { AutoBind } from "~/types/utils"; -import { utils } from "~/utils"; -import { validationCheckers } from "~/validationCheckers"; - -export class Validator { - protected errorChecker: ErrorChecker; - protected validationResult: ValidationResult; - protected value: any; - hasError = false; - - protected ignoredErrorTypesForInputValidator: ErrorTypeItem[] = [ - "required", - "stringEmpty", - "stringMin", - ]; - - constructor( - protected fieldName: Field, - protected compiledValidator: ValidatorType, - protected model: ValidationModel - ) { - this.errorChecker = validationCheckers[fieldName]; - } - - checkErrors() { - // eslint-disable-next-line promise/catch-or-return - try { - this.tryToCheckErrors(); - } catch (error) { - this.printErrors(error as NativeError[]); - } - - return this; - } - - @AutoBind - private tryToCheckErrors() { - if (Array.isArray(this.validationResult) && this.validationResult.length) - this.errorChecker(this.validationResult, this.value); - return this; - } - - @AutoBind - private printErrors(errors: NativeError[]) { - utils.printResponseErrors(errors); - return this; - } - - executeIfNoError(cb: (value: any) => void) { - if ( - Array.isArray(this.validationResult) && - this.validationResult.length === 0 - ) { - cb(this.value); - } - } -} - -export const validator = ( - fieldName: Field, - compiledValidator: ValidatorType, - model: ValidationModel -) => new Validator(fieldName, compiledValidator, model); diff --git a/apps/web/src/components/Common/Input/Select/CountrySelector/index.tsx b/apps/web/src/components/Common/Input/Select/CountrySelector/index.tsx index 670b299..7a19fad 100644 --- a/apps/web/src/components/Common/Input/Select/CountrySelector/index.tsx +++ b/apps/web/src/components/Common/Input/Select/CountrySelector/index.tsx @@ -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"; @@ -14,36 +13,12 @@ export type SelectCountryOnChange = VoidWithArg; interface Props { countryCode: string; countryName: string; - countryNameOnChange: OnChangeValidatorFn; - onSelectChange: SelectCountryOnChange; } -const CountrySelector: React.FC = ({ - countryCode, - countryName, - countryNameOnChange, - onSelectChange, -}) => { +const CountrySelector: React.FC = ({ 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) => (