Skip to content

Commit

Permalink
packages:js:fix-build
Browse files Browse the repository at this point in the history
  • Loading branch information
Voldemat committed Sep 9, 2023
1 parent c9a83c1 commit 7d336fb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 59 deletions.
5 changes: 1 addition & 4 deletions packages/js/src/index.ts
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'
61 changes: 6 additions & 55 deletions packages/js/src/validators/index.ts
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
)
}
}
}
56 changes: 56 additions & 0 deletions packages/js/src/validators/validators.ts
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
)
}
}
}

0 comments on commit 7d336fb

Please sign in to comment.