-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
59 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
export { | ||
type FormValidationSpec, | ||
type ValidatorError, | ||
type ValidatorFunc, | ||
buildValidator, | ||
type FormValidators, | ||
buildValidators | ||
buildValidator | ||
} from './validators' |
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 |
---|---|---|
@@ -1,56 +1,7 @@ | ||
import type { ImageTypeSpec, TypeSpec } from '../spec/types/type' | ||
import { | ||
successValidatorResult, | ||
type ValidationFormFieldSpec, | ||
type ValidatorFunc, | ||
type IValidatorResult | ||
export { buildValidator } from './validators' | ||
export type { | ||
ValidatorFunc, | ||
FormValidationSpec, | ||
ValidationFormFieldSpec, | ||
IValidatorResult | ||
} from './types' | ||
import { | ||
createValidationError, | ||
isImageValid, | ||
isScalarValueValid | ||
} from './utils' | ||
|
||
async function validateImage ( | ||
value: string | File | null, | ||
imageSpec: ImageTypeSpec, | ||
errorResult: IValidatorResult, | ||
successValidatorResult: IValidatorResult | ||
): Promise<IValidatorResult> { | ||
if (!(value instanceof File)) return errorResult | ||
const isValid = await isImageValid(value, imageSpec) | ||
return isValid | ||
? successValidatorResult | ||
: errorResult | ||
} | ||
|
||
export function buildValidator<T extends ImageTypeSpec> ( | ||
spec: ValidationFormFieldSpec<T> | ||
): (value: string | File | null) => Promise<IValidatorResult> | ||
export function buildValidator<T extends Exclude<TypeSpec, ImageTypeSpec>> ( | ||
spec: ValidationFormFieldSpec<T> | ||
): (value: string | File | null) => IValidatorResult | ||
export function buildValidator<T extends TypeSpec> ( | ||
spec: ValidationFormFieldSpec<T> | ||
): ValidatorFunc { | ||
const errorResult = createValidationError(spec.errorMessage) | ||
// eslint-disable-next-line @typescript-eslint/promise-function-async | ||
return (value: string | File | null) => { | ||
if (spec.required && value === null) return errorResult | ||
if (value === null) return errorResult | ||
if (spec.typeSpec.type !== 'image') { | ||
return ( | ||
isScalarValueValid(value, spec.typeSpec) | ||
? successValidatorResult | ||
: errorResult | ||
) | ||
} else { | ||
return validateImage( | ||
value, | ||
spec.typeSpec, | ||
errorResult, | ||
successValidatorResult | ||
) | ||
} | ||
} | ||
} |
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,56 @@ | ||
import type { ImageTypeSpec, TypeSpec } from '../spec/types/type' | ||
import { | ||
successValidatorResult, | ||
type ValidationFormFieldSpec, | ||
type ValidatorFunc, | ||
type IValidatorResult | ||
} from './types' | ||
import { | ||
createValidationError, | ||
isImageValid, | ||
isScalarValueValid | ||
} from './utils' | ||
|
||
async function validateImage ( | ||
value: string | File | null, | ||
imageSpec: ImageTypeSpec, | ||
errorResult: IValidatorResult, | ||
successValidatorResult: IValidatorResult | ||
): Promise<IValidatorResult> { | ||
if (!(value instanceof File)) return errorResult | ||
const isValid = await isImageValid(value, imageSpec) | ||
return isValid | ||
? successValidatorResult | ||
: errorResult | ||
} | ||
|
||
export function buildValidator<T extends ImageTypeSpec> ( | ||
spec: ValidationFormFieldSpec<T> | ||
): (value: string | File | null) => Promise<IValidatorResult> | ||
export function buildValidator<T extends Exclude<TypeSpec, ImageTypeSpec>> ( | ||
spec: ValidationFormFieldSpec<T> | ||
): (value: string | File | null) => IValidatorResult | ||
export function buildValidator<T extends TypeSpec> ( | ||
spec: ValidationFormFieldSpec<T> | ||
): ValidatorFunc { | ||
const errorResult = createValidationError(spec.errorMessage) | ||
// eslint-disable-next-line @typescript-eslint/promise-function-async | ||
return (value: string | File | null) => { | ||
if (spec.required && value === null) return errorResult | ||
if (value === null) return errorResult | ||
if (spec.typeSpec.type !== 'image') { | ||
return ( | ||
isScalarValueValid(value, spec.typeSpec) | ||
? successValidatorResult | ||
: errorResult | ||
) | ||
} else { | ||
return validateImage( | ||
value, | ||
spec.typeSpec, | ||
errorResult, | ||
successValidatorResult | ||
) | ||
} | ||
} | ||
} |