# Insert validation schema for one field based on another field

**URL:** <https://discuss.elastic.co/t/insert-validation-schema-for-one-field-based-on-another-field/363092>\
**Category:** Kibana\
**Created:** [July 14, 2024, 8:54am UTC](https://discuss.elastic.co/t/insert-validation-schema-for-one-field-based-on-another-field/363092 "2024-07-14T08:54:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzarei1996](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jzarei1996/32/124442_2.png) [@jzarei1996](https://discuss.elastic.co/u/jzarei1996)\
**Post date:** [July 14, 2024, 8:54am UTC](https://discuss.elastic.co/t/insert-validation-schema-for-one-field-based-on-another-field/363092/1 "2024-07-14T08:54:30Z")

</div>

Hi everyone.  
I have a structure like below which I want to validate according to what I will explain to you later.

```auto
router.post(
    {
      path: `${PLUGIN_API_PATH}/add`,
      validate: {
        body: schema.object({
          name: schema.string({ minLength: 1 }),
          description: schema.nullable(schema.string()),
          type: schema.oneOf([schema.literal('file'), schema.literal('link')]),
          data: schema.string({ minLength: 1 }),
          url: schema.string({ minLength: 1 }),
          format: schema.string({ minLength: 1 }),
          method: schema.string({ minLength: 1 }),
          authorization: schema.string({ minLength: 1 }),
          query: schema.string({ minLength: 1 }),
        }),
      },
    },
    async (context, request, response) => {
      .........
    }
  );

```

I want to bet based on the type field for data, url, etc. fields according to what I wrote.

```auto
router.post(
    {
      path: `${PLUGIN_API_PATH}/add`,
      validate: {
        body: schema.object({
          name: schema.string({ minLength: 1 }),
          description: schema.nullable(schema.string()),
          type: schema.oneOf([schema.literal('file'), schema.literal('link')]),
          data: if(type === 'file') ? schema.string({ minLength: 1 }) : schema.nullable(schema.string()),
          url: if(type === 'link') ? schema.string({ minLength: 1 }) : schema.nullable(schema.string()),
          format: if(type === 'link') ? schema.string({ minLength: 1 }) : schema.nullable(schema.string()),
          method: if(type === 'link') ? schema.string({ minLength: 1 }) : schema.nullable(schema.string()),
          authorization: if(type === 'link') ? schema.string({ minLength: 1 }) : schema.nullable(schema.string()),
          query: if(type === 'link') ? schema.string({ minLength: 1 }) : schema.nullable(schema.string()),
        }),
      },
    },
    async (context, request, response) => {
     ......
    }
  );

```

Basically, I want to validate one or more other fields based on the selected value for another field.

Is it possible?  
Please guide me how to do this.

Thanks in advance.
