So I'm trying to create yup validation with DTOs.
I get two errors:
- Yup argument is not assignable to parameter of type ObjectShape
- Property id is not assignable to index signature. Type number is not assignable to type 'Anyschema'
I have no clue whats wrong.If I remove CargoTypeDetailDto everything works perfect..
If the problems get fixed I have another question. The id is set by backend and does not need to be validated. Is there any other way to go about it other than yup.number().notRequired() ?
Here is the code:
import * as yup from 'yup'
import { CargoTypeDetailDto } from '../../../../../api/planning/open-api/models/CargoTypeDetailDto'
export const cargoTypeDetailDtoSchema = yup.object(<CargoTypeDetailDto>).shape({
id: yup.number().notRequired(),
code: yup
.string()
.min(1, 'Code can not be below 1')
.max(10, 'Code can not be above 10')
.required('Code is required'),
description: yup
.string()
.min(1, 'Description can not be below 1')
.max(255, 'Description can not be above 255')
.required('Description is required'),
priceCode: yup.object().shape({
code: yup.string().required('Article code is required'),
name: yup.string().required('Article description is required'),
}),
cargoTypeEuCode: yup.object().shape({
code: yup.string().required('EU code is required'),
description: yup.string().required('EU description is required'),
}),
})
The DTO:
import type { CargoTypeEuCodeDto } from './CargoTypeEuCodeDto';
import type { PriceCodeDto } from './PriceCodeDto';
export type CargoTypeDetailDto = {
id: number;
code: string;
description: string;
priceCode: PriceCodeDto;
cargoTypeEuCode: CargoTypeEuCodeDto;
};