All files / app/src/features/import/lib validation.ts

80.55% Statements 58/72
100% Branches 1/1
5.26% Functions 1/19
80.55% Lines 58/72

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 781x           1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x                     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
import * as yup from 'yup'
import { BaseEntity, TFunction } from '@/shared/@types'
import { getOptionalRequired, transformInputNumberValueToNumber, validateSelect } from '@/shared/lib'
import { AccountingType } from '@/entities/accounting-object'
import { MuftType } from '@/entities/pipe'
 
export const importCreateValidationSchema = (t: TFunction) =>
  yup.object({
    id: yup.string().required(),
    accountingObjectType: validateSelect(t),
    accountingType: validateSelect(t),
    connection: validateSelect(t),
    diameter: validateSelect(t),
    side: validateSelect(t),
    strengthGroup: validateSelect(t),
    contract: validateSelect(t),
    batch: yup.mixed().when('accountingType', {
      is: (accountingType: AccountingType) =>
        accountingType === AccountingType.BATCH ||
        accountingType === AccountingType.BATCH_PACKAGE_SINGLE ||
        accountingType === AccountingType.BATCH_SINGLE,
      then: schema =>
        schema.nullable().when({
          is: (value: string | BaseEntity) => typeof value === 'string' || value === null,
          then: schema => getOptionalRequired(schema, t, true),
          otherwise: schema =>
            getOptionalRequired(
              schema.transform((option: BaseEntity) => option['@id']),
              t,
              true
            ),
        }),
      otherwise: schema => schema.notRequired(),
    }),
    makeUpLengthLoss: transformInputNumberValueToNumber(t, true),
    makeUpLengthLossNotation: validateSelect(t),
    manufacturerCompany: validateSelect(t),
    manufacturer: validateSelect(t),
    affiliation: validateSelect(t),
    execution: validateSelect(t),
    priceTon: yup.mixed().nullable().notRequired(),
    priceMeter: yup.mixed().nullable().notRequired(),
    price: yup.mixed().nullable().notRequired(),
    priceCurrency: yup.mixed().when(['price'], {
      is: (price: string) => price,
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema => schema.notRequired(),
    }),
    lockPrice: yup.string().when(['price'], {
      is: (price: string) => price,
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema => schema.notRequired(),
    }),
    muftType: validateSelect(t),
    muftFask: yup.string().when('muftType', {
      is: (muftType: MuftType) => muftType === MuftType.Special || muftType === MuftType.Standard,
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema => schema.nullable().notRequired(),
    }),
    muftOuterDiameter: yup
      .number()
      .nullable()
      .transform(value => {
        return Number.isNaN(value) || value === null ? null : +value
      })
      .when('muftType', {
        is: (muftType: MuftType) => muftType === MuftType.Special,
        then: schema => schema.required(t('common:Required_field')),
        otherwise: schema => schema.notRequired(),
      }),
    preservationLubricant: validateSelect(t, false),
    preservationLubricantDate: yup.string().when('preservationLubricant', {
      is: (preservationLubricant: BaseEntity) => !!preservationLubricant,
      then: schema => schema.required(t('common:Required_field')),
      otherwise: schema => schema.notRequired(),
    }),
  })