Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Check all required properties are defined #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

piranna
Copy link

@piranna piranna commented Feb 2, 2018

This ensure that if a property is marked as required, that it exists in the definition of properties in a struct.

@gcanti
Copy link
Owner

gcanti commented Feb 2, 2018

@piranna specifying missing props wouldn't make for an invalid JSON schema in the first place?

@piranna
Copy link
Author

piranna commented Feb 2, 2018

It should, but this needs a higher level check because we need to check that some fields are available based on what's defined in another one, that's what my pull-request does... I don't think json schema is powerful enough to do that itself, and in fact currently I'm able to render invalid forms with non-existing required properties due to this.

@gcanti
Copy link
Owner

gcanti commented Feb 2, 2018

in fact currently I'm able to render invalid forms with non-existing required properties due to this

Let me understand, given this invalid schema

{
  "type": "object",
  "properties": {
    "a": { "type": "string" }
  },
  "required": ["b"]
}

is tcomb-json-schema somehow generating a type with both an a and b field?

Or, in another words, could you please show/add a test case in which tcomb-json-schema would produce a wrong type?

@piranna
Copy link
Author

piranna commented Feb 2, 2018

Let me understand, given this invalid schema

{
"type": "object",
"properties": {
"a": { "type": "string" }
},
"required": ["b"]
}
is tcomb-json-schema somehow generating a type with both an a and b field?

It's showing an a field marked as optional, b doesn't appear anywhere. My pull-request call to t.fail() saying that it didn't found b.

@gcanti
Copy link
Owner

gcanti commented Feb 5, 2018

I know this is a small change but I don't feel that tcomb-json-schema should validate the input schema

@piranna
Copy link
Author

piranna commented Feb 5, 2018

I know this is a small change but I don't feel that tcomb-json-schema should validate the input schema

Can you argument on this? I don't understand about why it shouldn't at least show a warning...

@gcanti
Copy link
Owner

gcanti commented Feb 5, 2018

Because validating the input schema is out of scope. This library should just convert a (valid) JSON Schema to tcomb types.

Otherwise why just stop here? There are plenty of other validations that could be done but then you end up replicating a json schema validator

@piranna
Copy link
Author

piranna commented Feb 5, 2018

I try to use defensive programming whenever is possible, I understand validation would be done in a previous step and maybe also in an independent module, but I tend to do checkings just before I'm going to use it, like it's this case, and just as last option if the data structure don't offer a safer option. For example, instead of a list of required field, each one would have a required or optional boolean flag instead, for example :-)

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

Successfully merging this pull request may close these issues.

2 participants