3 ms·
That ability to use outside canonical sources is really interesting. Are there some existing examples of schema languages with that feature?
by benjaminjackman 9y ago
That ability to use outside canonical sources is really interesting. Are there some existing examples of schema languages with that feature?
- crdoconnor 9y agoThe point I was making was that you shouldn't use a "special" language for validation at all - you should just use a library in a regular language to do it. Anyway, code: yaml_text: John: Yemen James: South Sudan python code: from strictyaml import load, MapPattern, Str, Enum import pycountry result = load( yaml_text, MapPattern( Str(), Enum([country.name for country in pycountry.countries]), ) ) full disclosure: I wrote the validation library ^^
- Someone 9y agoThe idea behind XML schema, DTD, etc. is to pick a simple language to express schemas in, so that implementations in different languages have a decent chance of being compatible with each other. Python isn’t a good choice there, as it is too flexible. For example, that code could have gotten the list of allowed country names from a file, database, or URL. ⇒ If I have to send such json to you, I almost would have to write my program in python, and even then, it could be hard for me to replicate your setup.
- crdoconnor 9y ago>that code could have gotten the list of allowed country names from a file, database, or URL. That is exactly the point. You should be able to do that, because the canonical list of data could easily from any of those and it should the up to programmer's discretion how to fetch it. The point of validation is to prevent invalid data from slipping through a net at minimum cost and that's how you do that. Suden, Sudaan and South Sudan were all invalid countries in 2010 and that YAML was invalid. In 2012, Suden and Sudaan were invalid but South Sudan was not so that YAML was valid. In the above example you have to make no code changes in order to account for that - just update pycountry every so often. With XML schemas and DTDs either you don't validate country at all (letting Suden and Sudaan) through the net. Or, you rewrite and redistribute the schema by hand every time some dependency like a list of countries changes. >If I have to send such json to you, I almost would have to write my program in python Only if I choose to validate that data using a shared schema. Frankly, I've dealt with XML a lot and the number of times I've been handed a shared schema of any kind is very low. People just don't seem to use them. If they define an API in XML for instance they tend to just send examples and give a written explanation (e.g. insert valid country name here). I don't see much value in making a schema more inherently "shareable" especially not if it means it has to be re-released every month.
- randallsquared 9y agoJSON Schema, at least, can refer to a URI for the definition of something, and that URI can refer to only a specific section of the JSON document to which it points.