From 59a0d0a9eb3137d3ae9143360d53d267a863381e Mon Sep 17 00:00:00 2001 From: Juan Esteban Arango Ossa Date: Fri, 28 Jun 2024 17:25:33 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=8F=20add=20required=20TP53maxvaf=20wh?= =?UTF-8?q?en=20TP53mut>0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/validation.ts | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/utils/validation.ts b/src/utils/validation.ts index 610d738..61c03a9 100644 --- a/src/utils/validation.ts +++ b/src/utils/validation.ts @@ -23,22 +23,28 @@ const buildSchema = (ipssrFields: FieldDefinition[]) => { break default: throw new Error(`Unsupported field type: ${field.type}`) - } + } + + fieldSchema = fieldSchema.messages({ + 'number.base': `'${field.label}' must be a number.`, + 'number.min': `'${field.label}' must be greater than or equal to ${field.min}${field.units}`, + 'number.max': `'${field.label}' must be less than or equal to ${field.max}${field.units}`, + 'any.required': `'${field.label}' is required`, + 'any.only': `'${field.label}' must be one of: ${field.values?.join(', ')}.`, + 'string.base': `'${field.label}' must be a string`, + }) - fieldSchema = fieldSchema.messages({ - 'number.base': `'${field.label}' must be a number.`, - 'number.min': `'${field.label}' must be greater than or equal to ${field.min}${field.units}`, - 'number.max': `'${field.label}' must be less than or equal to ${field.max}${field.units}`, - 'any.required': `'${field.label}' is required`, - 'any.only': `'${field.label}' must be one of: ${field.values?.join(', ')}.`, - 'string.base': `'${field.label}' must be a string`, + if (field.varName === 'TP53maxvaf') { + // Custom for Tp53maxvaf + fieldSchema = fieldSchema.when('TP53mut', { + is: Joi.exist().valid('1', '2 or more'), + then: Joi.required() }) - - if (field.required) { - fieldSchema = fieldSchema.required() - } else if ('default' in field) { - fieldSchema = fieldSchema.default(field.default) - } + } else if (field.required) { + fieldSchema = fieldSchema.required() + } else if ('default' in field) { + fieldSchema = fieldSchema.default(field.default) + } return schemaAcc.keys({ [field.varName]: fieldSchema }) }, Joi.object({}))