Using FeathersJS typebox schema how do I specify a conditionally required field?

505 views Asked by At

I am writing my first feathersjs application and I do not know how to specify a conditionally required field in the feathers schema (which is based on ajv and typebox). Basically, I have an 'isCompany' boolean field, and if that field is true, then 'companyName' should be required, otherwise it is optional (or even absent would be fine).

Pseudo code for what I want to accomplish:


// Main data model schema
export const userSchema = Type.Object(
  {
    _id: ObjectIdSchema(),
    email: Type.String({ format: 'email' }),
    password: Type.String({minLength: 6}),
    
    
    isCompany: Type.Boolean(),
    companyName: Type.STRING_THAT_IS_ONLY_REQUIRED_WHEN("isCompany == true"), // <-- this bit is what I need help on.
    
    
  },
  { $id: 'User', additionalProperties: false }
)

This is a very common use case, so I'm sure it is simple to solve, but I can't for the life of me find a solution after hours of pouring over the typebox and feathersjs examples, googling and testing.

0

There are 0 answers