Skip to content
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

Support required in nested object #2065

Open
PH-GBGPLC opened this issue Nov 25, 2019 · 7 comments
Open

Support required in nested object #2065

PH-GBGPLC opened this issue Nov 25, 2019 · 7 comments

Comments

@PH-GBGPLC
Copy link

PH-GBGPLC commented Nov 25, 2019

It would be good to be able to specify required fields in an object at a deeper level in the structure.
e.g. required: [objectName.elementName]. This would make it much easier to use allOf with modeled objects which have different required fields in different use cases.

@cebe
Copy link
Contributor

cebe commented Dec 4, 2019

Can you give an example? You can define required properties on all levels:

{
  "type": "object",
  "required": [
    "name", 
    "subobject"
  ],
  "properties": {
    "name": {
      "type": "string"
    },
    "subobject": {
      "type": "object",
      "required": [
        "id", 
      ],
      "properties": {
         "id": {
           "type": "number"
        },
    }
  }
}

@PH-GBGPLC
Copy link
Author

PH-GBGPLC commented Dec 4, 2019

My scenario is to do with where I have an object defined externally and then want to ref it in and declare something within it as required.
So in my models I may have something like:

components:  
	schemas:  
		ProblemSample:  
			object1:  
				type:object  
				properties:
					property1:
						type: string
					innerObject:
						type: object
						properties:
							property2:
								type: string

and then when I use it set specific required fields:

allOf:
	- $ref: '#/components/schemas/ProblemSample'
	- required:
		-	object1.property1
		-	object1.innerobject.property2

but in a different use I want the same model but a different set of required fields.

@handrews
Copy link
Contributor

This has come up in the context of JSON Schema- I think the proposals were named deepProperties and deepRequired. The proposed syntax was Relative JSON Pointer rather than JavaScript dotted object notation, in keeping with JSON Schema's general reliance on JSON Pointers.

The proposals have not been added to JSON Schema in part because they are easily implemented with a preprocessor that can be run in a build step (they simply expand to nested schemas using properties and required). They are likely candidates for extension vocabularies in JSON Schema draft 2019-09 / OpenAPI 3.1.

@andersk
Copy link

andersk commented Sep 4, 2020

You can already do this:

allOf:
  - $ref: "#/components/schemas/ProblemSample"
  - properties:
      object1:
        required: [property1]
        properties:
          innerobject:
            required: [property2]

(Perhaps you also want to mark object1 and object1.innerobject themselves as required.)

FTR, the corresponding JSON Schema issue is json-schema-org/json-schema-vocabularies#18.

@vinod4dev
Copy link

deeprequired helps with better object reusability . One could declare a common object with all fields optional and reuse that in the overall schema in combination with allOf keyword whilst making some fields mandatory . Right now the only option is to declare multiple objects .

Example -----
User :
first_name:
last _name:

API ------
/putUser
properties :
someotherfield:
$ref : SomeOtherObject
user :
$ref : User
required :
- user.first_name
- user.last_name

/updateUser
properties :
user :
$ref : User

/someOtherUserApi
properties :
user :
$ref : User
required :
- user.first_name

@LasneF
Copy link

LasneF commented Jan 29, 2024

@handrews , i think this one could be closed as a handled / linked to the Json Schema repository
json-schema-org/json-schema-vocabularies#18

@handrews
Copy link
Contributor

@LasneF I've paused closing JSON Schema issues as I want to get a clear decision on whether we're drawing a hard boundary here or not. For now i'm making sure they're all tagged with schema-object (including proposal for JSON Schema alternatives).

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

No branches or pull requests

6 participants