-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Illiar/feat/kyb validation poc (DRAFT) #2578
Open
chesterkmr
wants to merge
5
commits into
dev
Choose a base branch
from
illiar/feat/kyb-validation-poc
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5ca2bc
feat: initial boilerplate
chesterkmr 28fc959
feat: implemented initial version of validator & added example
chesterkmr 43dd5d4
fix: fixed exception
chesterkmr cb03f45
feat: added tests & added max length validator
chesterkmr 030b4f9
feat: implemented pattern validator & tests
chesterkmr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//@ts-nocheck | ||
|
||
import { Validator } from '@/components/providers/Validator'; | ||
import { pocDefinition } from '@/poc-definition'; | ||
import { FieldList } from '@/poc/FieldList'; | ||
import { SubmitButton } from '@/poc/SubmitButton'; | ||
import { TextInput } from '@/poc/TextInput'; | ||
|
||
const context = { | ||
items: [ | ||
{ | ||
subItems: [{}, { subsubItems: [] }], | ||
}, | ||
{}, | ||
], | ||
}; | ||
|
||
export const ValidatorPOC = () => { | ||
return ( | ||
<Validator elements={pocDefinition} context={context}> | ||
<form | ||
onSubmit={e => { | ||
e.preventDefault(); | ||
alert('submitted'); | ||
}} | ||
> | ||
<TextInput context={context} element={pocDefinition[0]!} stack={[]} /> | ||
<TextInput context={context} element={pocDefinition[1]!} stack={[]} /> | ||
<div className="pl-8"> | ||
<FieldList context={context} element={pocDefinition[2]!}> | ||
<div className="flex flex-col"> | ||
<TextInput context={context} element={pocDefinition[2]!.children[0]!} stack={[0]} /> | ||
<TextInput context={context} element={pocDefinition[2]!.children[1]!} stack={[0]} /> | ||
</div> | ||
<div className="flex flex-col"> | ||
<TextInput context={context} element={pocDefinition[2]!.children[0]!} stack={[1]} /> | ||
<TextInput context={context} element={pocDefinition[2]!.children[1]!} stack={[1]} /> | ||
</div> | ||
<div className="pl-8"> | ||
<FieldList context={context} element={pocDefinition[2]!.children[2]!} stack={[0]}> | ||
<div className="flex flex-col"> | ||
<TextInput | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[0]!} | ||
stack={[0, 0]} | ||
/> | ||
<TextInput | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[1]!} | ||
stack={[0, 0]} | ||
/> | ||
</div> | ||
<div className="flex flex-col"> | ||
<TextInput | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[0]!} | ||
stack={[0, 1]} | ||
/> | ||
<TextInput | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[1]!} | ||
stack={[0, 1]} | ||
/> | ||
</div> | ||
<FieldList | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[2]!} | ||
stack={[0, 1]} | ||
> | ||
<div className="flex flex-col"> | ||
<TextInput | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[2]!.children[0]!} | ||
stack={[0, 0]} | ||
/> | ||
<TextInput | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[2]!.children[1]!} | ||
stack={[0, 0]} | ||
/> | ||
</div> | ||
<div className="flex flex-col"> | ||
<TextInput | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[2]!.children[0]!} | ||
stack={[0, 1]} | ||
/> | ||
<TextInput | ||
context={context} | ||
element={pocDefinition[2]!.children[2]!.children[2]!.children[1]!} | ||
stack={[0, 1]} | ||
/> | ||
</div> | ||
</FieldList> | ||
</FieldList> | ||
</div> | ||
</FieldList> | ||
</div> | ||
<SubmitButton /> | ||
</form> | ||
</Validator> | ||
); | ||
}; |
41 changes: 41 additions & 0 deletions
41
apps/kyb-app/src/components/providers/Validator/Validator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useValidate } from '@/components/providers/Validator/hooks/useValidate'; | ||
import { UIElement } from '@/components/providers/Validator/hooks/useValidate/ui-element'; | ||
import { UIElementV2 } from '@/components/providers/Validator/types'; | ||
import React, { FunctionComponent, useCallback, useMemo, useState } from 'react'; | ||
import { TValidationErrors, validatorContext } from './validator.context'; | ||
|
||
const { Provider } = validatorContext; | ||
|
||
export interface IValidatorProps { | ||
children: React.ReactNode | React.ReactNode[]; | ||
context: unknown; | ||
elements: UIElementV2[]; | ||
} | ||
|
||
export const Validator: FunctionComponent<IValidatorProps> = ({ children, elements, context }) => { | ||
const validate = useValidate({ elements, context }); | ||
const [validationErrors, setValiationErrors] = useState<TValidationErrors>({}); | ||
|
||
const onValidate = useCallback(() => { | ||
const errors = validate(); | ||
const validationErrors = errors.reduce((acc, error) => { | ||
const element = new UIElement(error.element, context, error.stack); | ||
acc[element.getId()] = error.message; | ||
|
||
return acc; | ||
}, {} as TValidationErrors); | ||
|
||
setValiationErrors(validationErrors); | ||
|
||
console.log({ errors, validationErrors }); | ||
|
||
return Boolean(errors.length); | ||
}, [validate]); | ||
|
||
const ctx = useMemo( | ||
() => ({ validate: onValidate, errors: validationErrors }), | ||
[validationErrors, onValidate], | ||
); | ||
|
||
return <Provider value={ctx}>{children}</Provider>; | ||
}; |
1 change: 1 addition & 0 deletions
1
apps/kyb-app/src/components/providers/Validator/hooks/useValidate/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useValidate'; |
116 changes: 116 additions & 0 deletions
116
apps/kyb-app/src/components/providers/Validator/hooks/useValidate/ui-element.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { UIElementV2 } from '@/components/providers/Validator/types'; | ||
import { | ||
IMinLengthValueValidatorParams, | ||
MinLengthValueValidator, | ||
} from '@/components/providers/Validator/value-validators/min-length.value.validator'; | ||
import { | ||
IRequiredValueValidatorParams, | ||
RequiredValueValidator, | ||
} from '@/components/providers/Validator/value-validators/required.value-validator'; | ||
import get from 'lodash/get'; | ||
|
||
export class UIElement { | ||
constructor(readonly element: UIElementV2, readonly context: unknown, readonly stack: number[]) {} | ||
|
||
getId() { | ||
return this.formatId(this.element.id); | ||
} | ||
|
||
getOriginId() { | ||
return this.element.id; | ||
} | ||
|
||
private formatId(id: string) { | ||
return `${id}${this.stack.join('.')}`; | ||
} | ||
|
||
getValueDestination() { | ||
return this.formatValueDestination(this.element.valueDestination); | ||
} | ||
|
||
private formatValueDestination(valueDestination: string) { | ||
return this.formatValueAndApplyStackIndexes(valueDestination); | ||
} | ||
|
||
private formatValueAndApplyStackIndexes(value: string) { | ||
this.stack.forEach((stackValue, index) => { | ||
value = value.replace(`$${index}`, String(stackValue)); | ||
}); | ||
|
||
return value; | ||
} | ||
|
||
getValue() { | ||
const valueDestination = this.getValueDestination(); | ||
|
||
return get(this.context, valueDestination); | ||
} | ||
|
||
getValidatorsParams() { | ||
const validatorKeys = Object.keys(this.element.validation); | ||
|
||
return validatorKeys.map(key => ({ | ||
validator: key, | ||
params: this.getValidatorParams(key), | ||
})); | ||
} | ||
|
||
private getValidatorParams(key: string) { | ||
switch (key) { | ||
case 'minLength': | ||
return this.getMinLengthParams(); | ||
case 'required': | ||
return this.getRequiredParams(); | ||
default: | ||
throw new Error('Invalid key'); | ||
} | ||
} | ||
|
||
private getMinLengthParams(): IMinLengthValueValidatorParams { | ||
const _params = this.element.validation.minLength; | ||
|
||
if (MinLengthValueValidator.isMinLengthParams(_params)) { | ||
if (Array.isArray(_params)) { | ||
return { | ||
minLength: _params[0], | ||
message: _params[1], | ||
}; | ||
} | ||
|
||
const minLength = _params; | ||
|
||
const params: IMinLengthValueValidatorParams = { | ||
minLength, | ||
message: `Minimum length is ${minLength}.`, | ||
}; | ||
|
||
return params; | ||
} | ||
|
||
throw new Error('Invalid params'); | ||
} | ||
|
||
private getRequiredParams(): IRequiredValueValidatorParams { | ||
const _params = this.element.validation.required; | ||
|
||
if (RequiredValueValidator.isRequiredParams(_params)) { | ||
if (Array.isArray(_params)) { | ||
return { | ||
required: _params[0], | ||
message: _params[1], | ||
}; | ||
} | ||
|
||
const isRequired = _params; | ||
|
||
const params: IRequiredValueValidatorParams = { | ||
required: isRequired, | ||
message: `Value is required.`, | ||
}; | ||
|
||
return params; | ||
} | ||
|
||
throw new Error('Invalid params'); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the typo in the state setter function name.
The state setter function
setValiationErrors
should besetValidationErrors
.Committable suggestion