Skip to content

Commit d7eeb22

Browse files
authored
fix: Add valibot transformers for numeric and boolean fields in schemas (#189)
1 parent 3e2628e commit d7eeb22

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

src/models/course/course.schemas.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { status } from './course.entity.js';
77
import { QuestionType } from '../question/question.entity.js';
88

99
const NumericString = v.pipe(
10-
v.string('El valor debe ser un string.'),
11-
v.regex(/^\d+$/, 'Debe contener solo dígitos.'),
12-
v.transform(Number)
10+
v.union([v.string(), v.number()]),
11+
v.transform((input) => Number(input)),
12+
v.integer('El valor debe ser un entero.'),
13+
v.minValue(0, 'El valor no puede ser negativo.')
1314
);
1415

1516
const BooleanString = v.pipe(
16-
v.string('El valor debe ser un string.'),
17-
v.regex(/^(true|false)$/, 'El valor debe ser "true" o "false".'),
18-
v.transform((val) => val === 'true')
17+
v.union([v.string(), v.boolean()]),
18+
v.transform((val) => val === 'true' || val === true)
1919
);
2020

2121
const QuestionUpdateSchema = v.object({
@@ -56,21 +56,13 @@ export const CreateCourseSchema = v.object({
5656
v.string(),
5757
v.minLength(1, 'Course description is required.')
5858
),
59-
priceInCents: v.optional(
60-
v.pipe(
61-
v.number('Price must be a number.'),
62-
v.integer('Price in cents must be an integer.'),
63-
v.minValue(0, 'Price cannot be negative.')
64-
)
65-
),
66-
isFree: v.optional(v.boolean()),
67-
59+
priceInCents: v.optional(NumericString),
60+
isFree: v.optional(BooleanString),
6861
courseTypeId: v.pipe(
6962
v.string(),
7063
v.minLength(1, 'Course type ID is required.')
7164
),
7265
units: v.optional(v.array(UnitSchema)),
73-
7466
useInstitution: v.optional(BooleanString),
7567
status: v.optional(
7668
v.picklist(Object.values(status), 'The provided status is not valid.')

0 commit comments

Comments
 (0)