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
ignore inherited and non-enumerable object properties #78
Conversation
One more note: The |
@hegemonic: a contributor here: about the index.html: it is regenerated from the README.md. I see the |
This is an interesting point. JSON Schema was intended for JSON data, which doesn't have issues with inherited or non-enumerable properties, so this is in some ways a bit of a grey area. Personally, my instinct is that if a property is accessible, then it's reasonable to validate it. I mean, if I had: DataMaker.prototype.description = "(no description yet)"; but my schema said: {
"properties": {
"description": {"type": "integer"}
}
} should that be valid? I mean, the property exists and is of an incorrect type, so I'd say "no, that's invalid". I think I feel similarly about @Bartvds: how do you feel about this? |
@geraintluff, I would counter by pointing out that Perhaps I should explain my use case as well: JSDoc 3 creates "doclet" objects that describe symbols in JavaScript files. Each doclet is an instance of a To enhance our test suite, I'm creating a JSON schema that I can use to validate the doclets. With the current version of |
@hegemonic: that is a good point about OK, so a behaviour change like this seems like it would deserve a minor version bump. Is it worth pro-actively putting in a setting to maintain the old behaviour, or shall we just wait until someone actually requests it? |
@geraintluff: Totally up to you. If you'd like a setting to maintain the old behavior, just tell me what to call it and how you want to expose the setting. |
Interesting issue. I think by default inherited properties should be validated just the same as own properties. Accessing a property (eg: On the other hand, just like @hegemonic I like to use json-schema on JS objects, and the For the option I think we should move to an 'options object' for the Also now we are on the topic: using json-schema on JS objects could use a few more schema rules/flags. Like
So maybe we should look at the schema-rule part of this in a slightly wider scope and see if |
I just noticed some new comments came in after I started to reply. The Even so consider this a +1 for a way to keep current behaviour accessible (even if it wouldn't be the default), it is especially handy to validate JS values (where inherited non-enumerable properties are important). |
Got it. I'm happy to make this configurable. I can also update the Just let me know what you'd like to call the option(s) for ignoring inherited and non-enumerable properties. |
An It might also be good to be able to modify the default options: tv4.options({ownPropertiesOnly: true}); // merges against existing defaults
var freshTv4 = tv4.freshApi({language: 'de'}); // resets everything (schema cache, settings, etc.)
tv4.validate(data, schema, {checkRecursive: true}); // per-run options @hegemonic: I apologise for the feature creep on this one. I don't want you to feel like your code (and your very useful question/insight) is not appreciated! If you'd rather, then this can be pulled into a separate branch and I/we can add the (Also, I should definitely add a |
@geraintluff The options as third argument sounds good to me, I really like being able to set all of them both as default as well as per validation run. I don't know in which order to add that with @hegemonic's changes, whatever is practical I guess. |
@geraintluff: No worries about the feature creep...sometimes one idea leads to another. Here's how I'd like to proceed:
I should be able to do this within the next several days. After that change is merged, you and @Bartvds can implement the other changes you discussed. Does that sound good? |
@hegemonic Note there are also @geraintluff Maybe if tv4 needs to move to a new non-patch release because signatures change anyway it is a good moment to drop this statefull |
@hegemonic: that sounds great, thanks! @Bartvds: I agree it's not great. Remove it completely, or just drop the stateful aspects and return a boolean? |
@Bartvds: I'll be sure to address |
@geraintluff I have no preference. Could be convenient to keep it as simple shorthand with just a boolean if it is documented as such. |
Okay, I've taken care of the changes we discussed. I decided to use two separate options for inherited and non-enumerable properties, on the theory that anyone who needs those options will want really fine-grained control over validation. I also added documentation for the two new options, along with banUnknownProperties, which was not previously documented. The new docs are a bit repetitive, but they get the job done. Please let me know if you have any additional feedback before you merge this. |
@geraintluff have you had a chance to look at this? |
@Bartvds - I'm afraid this had completely slipped off my radar. The changes look reasonable, though - unfortunately, I think I've checked in some incompatible ones since, so the merge'll have to be manual... |
@geraintluff: What can I do to help you merge this? Would you like me to rebase the changes off the current master? |
@hegemonic - if you could that would be fantastic. :) |
@geraintluff: Done! Hope this is now ready to merge. |
It looks like some commits landed after you restarted the work: the merge is disabled (and the diff is odd). So I think you need another rebase to solve the conflict. |
Populate dataPath for required properties
We created a Sorry for the confusion, but we're juggling a few simultaneous PR's so please bear with us (your feature is very welcome! :) |
@Bartvds: Will do. I'll create a new pull request if necessary. |
fixes issues when additionalProperties == false
I'm going to close this pull request and submit a new one that targets the |
The current version of
tv4
includes several methods that iterate over object properties without filtering out inherited and non-enumerable properties. As a result, thedata
object fails validation in the following example, becausedata
inherits theextraMethod
property:This pull request fixes the issue by iterating over the return value of
Object.keys(obj)
rather than callingfor (var prop in obj)
. If you'd prefer to fix this issue in a different way, I'd be happy to close this pull request and submit a new one.