jdorn / json-editor

JSON Schema Based Editor
MIT License
5.81k stars 1.08k forks source link

uniqueTemplate for Arrays #191

Open yokto opened 10 years ago

yokto commented 10 years ago

I don't know if this is already possible but it would be cool to have a way of saying exactly what part of an array object should be unique. Usually, it's not the whole object that needs to be unique, but just one or two properties

brettz9 commented 10 years ago

See https://github.com/json-schema/json-schema/ for making spec requests or bug reports, or, if just asking questions like these, there is a tag at http://stackoverflow.com/tags/jsonschema

yokto commented 10 years ago

As I see it, all template related features are only in this implementation and not in the standard.

brettz9 commented 10 years ago

From all I can tell you are asking about something relevant to validation, and only secondarily relevant to rendering. JSON Editor does have some custom properties related to rendering, but uniqueItems is part of the spec, and presumably any tweaking of it (not pertaining to rendering) would also be related to the spec.

yokto commented 10 years ago

ok but the first_name last_name example also verifies that full_name is first_name last_name. This is enforced. You could say that the frontend just doesn't allow you to input a different value. But equivalently the frontend could not allow you to input two array elements with the same name field. I don't see the difference.

mazswojejzony commented 10 years ago

Let me just say this would be a really nice feature!

AFAIK it is not possible to define in the schema that one of array objects property shall be unique. For example you cannot say you do not want 2 or more people with the same last_name.

brettz9 commented 10 years ago

While JSON Schema will not be able to accommodate every possible type of validation, the first stop for validation requests that may be of general interest should be changes to the spec! It is a good idea, so why not suggest it for the spec? (or better, ask the question first on the mailing list or prominent forum to make sure the idea hasn't already been accepted or dismissed (looks like something similar existed previously), and then, if it is not being supported, report back on how there is still a custom need).

Even if JSON Editor adds its own validation properties (and a read-only magic copy of other properties through watch is not exactly validation to me, but regardless, it doesn't even matter), it is still ideal to have it as part of the spec. JSON Editor is not the only tool benefiting from JSON Schema. A big part of the appeal of JSON Editor is that it relies on a standard so that one's data can be immediately readable to a well-informed developer audience and so that more kinds of tools can work out-of-the-box and inter-operate on the same data.

Of course, it's not my call, but this just seems like the wrong forum for this discussion even while I think it is a good discussion to have.

mazswojejzony commented 10 years ago

Good suggestion, discussion has moved to the following thread: https://groups.google.com/forum/#!topic/json-schema/CYd8xk1Czyg

yingliangzhang commented 7 years ago

You could use this little custom validator that I wrote https://gist.github.com/yingliangzhang/63d3373cf91991898a75810bee140038

Then just add "uniqueFields": ["field1", "field2"], to your array property in your schema.json