mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-02 05:23:14 +00:00
chore: properly type pattern error
This commit is contained in:
committed by
Aiden McClelland
parent
e0ee89bdd9
commit
fecbae761e
@@ -19,30 +19,14 @@ import { TUI_VALIDATION_ERRORS } from '@taiga-ui/kit'
|
|||||||
import { filter, takeUntil } from 'rxjs'
|
import { filter, takeUntil } from 'rxjs'
|
||||||
import { ValueSpec, ValueSpecText } from 'start-sdk/lib/config/configTypes'
|
import { ValueSpec, ValueSpecText } from 'start-sdk/lib/config/configTypes'
|
||||||
import { ERRORS } from '../form-group/form-group.component'
|
import { ERRORS } from '../form-group/form-group.component'
|
||||||
|
import { FORM_CONTROL_PROVIDERS } from './form-control.providers'
|
||||||
interface ValidatorsPatternError {
|
|
||||||
actualValue: string
|
|
||||||
requiredPattern: string | RegExp
|
|
||||||
}
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'form-control',
|
selector: 'form-control',
|
||||||
templateUrl: './form-control.component.html',
|
templateUrl: './form-control.component.html',
|
||||||
styleUrls: ['./form-control.component.scss'],
|
styleUrls: ['./form-control.component.scss'],
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
providers: [
|
providers: FORM_CONTROL_PROVIDERS,
|
||||||
{
|
|
||||||
provide: TUI_VALIDATION_ERRORS,
|
|
||||||
deps: [FormControlComponent],
|
|
||||||
useFactory: (control: FormControlComponent<ValueSpecText, string>) => ({
|
|
||||||
required: 'Required',
|
|
||||||
pattern: ({ requiredPattern }: ValidatorsPatternError) =>
|
|
||||||
control.spec.patterns.find(
|
|
||||||
({ regex }) => String(regex) === String(requiredPattern),
|
|
||||||
)?.description || 'Invalid format',
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})
|
})
|
||||||
export class FormControlComponent<
|
export class FormControlComponent<
|
||||||
T extends ValueSpec,
|
T extends ValueSpec,
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { forwardRef, Provider } from '@angular/core'
|
||||||
|
import { TUI_VALIDATION_ERRORS } from '@taiga-ui/kit'
|
||||||
|
import { ValueSpec } from 'start-sdk/lib/config/configTypes'
|
||||||
|
import { FormControlComponent } from './form-control.component'
|
||||||
|
|
||||||
|
interface ValidatorsPatternError {
|
||||||
|
actualValue: string
|
||||||
|
requiredPattern: string | RegExp
|
||||||
|
}
|
||||||
|
|
||||||
|
export const FORM_CONTROL_PROVIDERS: Provider[] = [
|
||||||
|
{
|
||||||
|
provide: TUI_VALIDATION_ERRORS,
|
||||||
|
deps: [forwardRef(() => FormControlComponent)],
|
||||||
|
useFactory: (control: FormControlComponent<ValueSpec, string>) => ({
|
||||||
|
required: 'Required',
|
||||||
|
pattern: ({ requiredPattern }: ValidatorsPatternError) =>
|
||||||
|
('patterns' in control.spec &&
|
||||||
|
control.spec.patterns.find(
|
||||||
|
({ regex }) => String(regex) === String(requiredPattern),
|
||||||
|
)?.description) ||
|
||||||
|
'Invalid format',
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user