0

I want to check: If my schema.type ==='gas' then my schema.status should be good,
if schema.type = noGas then my schema.status should be bad.
Both fields are mandatory.

I was able to make a field mandatory or not based on another field value.

const bottle: Schema = new Schema({
  type: {
    type: String,
    required: true,
    enum: ['gas', 'noGas'],
  },
  status: {
    type: String,
    enum: ['good', 'bad']
    required(this: IBottle): boolean {
      // Check values here. But I could only ensure the field as required or not.  
      return this.type === 'gas' || this.type === 'noGas';
    }

  }
});

But what if besides that, I also want to validate the value of the field, not just if it is present.

How could I do that?

I saw this example, which is using Validate() to validate the value, but i need a little more. I need to also SET the field's value, based on the value of the other field.

PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
  • Does this answer your question? [mongoose custom validation using 2 fields](https://stackoverflow.com/questions/23760253/mongoose-custom-validation-using-2-fields) – Mostafa Fakhraei Dec 26 '22 at 18:40
  • Actually it is doing almost the same thing I showed above, if I understood right. I believe there is no way to set a field value based on the value of another field. Only use boolean functions to validate and costumize errors. – PlayHardGoPro Dec 26 '22 at 23:23

0 Answers0