All files / app/src/features/measure/lib validate.ts

2.25% Statements 3/133
100% Branches 0/0
0% Functions 0/2
2.25% Lines 3/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 1391x         1x                                                                                                                                                                                                                                                           1x              
import { BaseEntity, TFunction } from '@/shared/@types'
import * as yup from 'yup'
import { getOptionalRequired, validateSelect } from '@/shared/lib'
import { MAXIMUM_BACKEND_NUMBER, MINIMUM_BACKEND_NUMBER } from '@/shared/config'
 
export const validateMeasure = (t: TFunction) =>
  yup.object({
    contract: validateSelect(t),
    connection: validateSelect(t),
    diameter: validateSelect(t),
    side: validateSelect(t),
    strengthGroup: validateSelect(t),
    status: yup.array().min(1, t('common:Required_field')).required(t('common:Required_field')),
    requiredLength: yup.mixed().when('measureType', {
      is: (value: string) => value === 'length',
      then: schema =>
        getOptionalRequired(
          schema
            .nullable()
            .transform(value => {
              return Number.isNaN(value) || value === null || !value ? null : +value
            })
            .when({
              is: (value: unknown) => typeof value === 'number',
              then: () =>
                yup
                  .number()
                  .min(MINIMUM_BACKEND_NUMBER, t('common:The_number_cannot_be_less_than'))
                  .max(MAXIMUM_BACKEND_NUMBER, t('common:The_number_cannot_be_greater_than')),
            }),
          t,
          true
        ),
      otherwise: schema => schema.notRequired(),
    }),
    requiredLengthNotation: yup.string().when('measureType', {
      is: 'length',
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema => schema.notRequired(),
    }),
    overrideCurrency: yup.string().when('priceOverride', {
      is: (value: string) => value,
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema =>
        schema.nullable().when({
          is: (value: string | BaseEntity) => typeof value === 'string' || value === null,
          then: schema => getOptionalRequired(schema, t, false),
          otherwise: schema =>
            getOptionalRequired(
              schema.transform((option: BaseEntity) => option['@id']),
              t,
              false
            ),
        }),
    }),
    overrideMakeUpLengthLossNotation: yup.string().when('overrideMakeUpLengthLoss', {
      is: (value: string) => value,
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema =>
        schema.nullable().when({
          is: (value: string | BaseEntity) => typeof value === 'string' || value === null,
          then: schema => getOptionalRequired(schema, t, false),
          otherwise: schema =>
            getOptionalRequired(
              schema.transform((option: BaseEntity) => option['@id']),
              t,
              false
            ),
        }),
    }),
    lengthScaleNotation: yup.string().when('length', {
      is: (value: string[]) => {
        const isAllEmpty = value?.every(item => !item)
        return !!value?.length && !isAllEmpty
      },
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema =>
        schema.nullable().when({
          is: (value: string | BaseEntity) => typeof value === 'string' || value === null,
          then: schema => getOptionalRequired(schema, t, false),
          otherwise: schema =>
            getOptionalRequired(
              schema.transform((option: BaseEntity) => option['@id']),
              t,
              false
            ),
        }),
    }),
    priceCurrency: yup.string().when('price', {
      is: (value: string[]) => {
        const isAllEmpty = value?.every(item => !item)
        return !!value?.length && !isAllEmpty
      },
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema =>
        schema.nullable().when({
          is: (value: string | BaseEntity) => typeof value === 'string' || value === null,
          then: schema => getOptionalRequired(schema, t, false),
          otherwise: schema =>
            getOptionalRequired(
              schema.transform((option: BaseEntity) => option['@id']),
              t,
              false
            ),
        }),
    }),
    requiredItemsCount: yup.mixed().when('measureType', {
      is: (value: string) => value === 'items',
      then: schema =>
        getOptionalRequired(
          schema
            .nullable()
            .transform(value => {
              return Number.isNaN(value) || value === null || !value ? null : +value
            })
            .when({
              is: (value: unknown) => typeof value === 'number',
              then: () =>
                yup
                  .number()
                  .min(MINIMUM_BACKEND_NUMBER, t('common:The_number_cannot_be_less_than'))
                  .max(MAXIMUM_BACKEND_NUMBER, t('common:The_number_cannot_be_greater_than')),
            }),
          t,
          true
        ),
      otherwise: schema => schema.notRequired(),
    }),
    priceOverride: yup.string().nullable().notRequired(),
  })
 
export const measureSaveValidationSchema = (t: TFunction) =>
  yup.object({
    name: yup.string().required(t('common:Required_field')),
    deposit: validateSelect(t, false),
    bush: validateSelect(t, false),
    well: validateSelect(t, false),
  })