Skip to content

Question: 'required' fields based on context #1562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kolisko opened this issue Apr 24, 2018 · 4 comments
Closed

Question: 'required' fields based on context #1562

kolisko opened this issue Apr 24, 2018 · 4 comments

Comments

@kolisko
Copy link

kolisko commented Apr 24, 2018

Hi,

here is the definition of Dog:

Dog:
  properties:
    name:
      type: string
    breed:
      type: string

there is no required fields.

Now I would like to reference the Dog definition in different contexts and want add/modify required parameters on Dog definition. Something like this:

LoadRequest:
  type: object
  properties:
    required:
      - **breed**
    dog:
      $ref: "#/definitions/Dog"

SaveRequest:
  type: object
  properties:
    required:
      - **breed**
      - **name**
    dog:
      $ref: "#/definitions/Dog"

Main idea is to have one Object definition and many references with different required fields. Is possible to define required field based on reference somehow? Or somehow else? I know that one solution is to define three Dogs :/ - every Dog with different required fields. But it is lot of duplication and in real the object Dog is much more bigger. Any idea?

Thanks
kolisko

@MikeRalphson
Copy link
Member

There is a similar query here.

You can use the allOf keyword to extend your Dog definition. Assuming LoadRequest and saveRequest are Dogs, rather than having dog as a property:

LoadRequest:
  allOf:
    - $ref: "#/definitions/Dog"
    - required:
      - breed

SaveRequest:
  allOf:
    - $ref: "#/definitions/Dog"
    - required:
      - breed
      - name

In essence, allOf requires that the instance validates against all of the schemas in the given array. The first (the $ref to Dog) requires that if present, name and breed are strings, but neither are required. The second schema in the array enforces that the given properties must be present (but not what types they are). In combination, they give you the validation you're looking for.

In OpenAPI 3.0.x #/definitions/... would be #/components/schemas/... (assuming a self-contained OAS document).

@hkosova
Copy link
Contributor

hkosova commented Apr 25, 2018

@jukeblimp32
Copy link

In my API definition I'm using allOf and required just as @MikeRalphson suggested. Everything is working great so far, but I have a question about requiring nested properties. Let's say that in the example above, that the breed property is actually another object that has properties for breed_name, size, and breed_origin.

How would I be able to specify, using the strategy above, that the nested property breed_name is required?

@handrews
Copy link
Member

The original question in this issue was answered in 2018, subsequent questions if still relevant should be filed as their own issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants