XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] How to validate JSON against XSD schemas

On Mon, 2018-07-16 at 12:43 -0700, Piers MacDonald wrote:
> I know the title sounds impossible, give it a second. We have XSD
> schemas
> created by an open-standards industry group. In our service we'd like
> our
> customers to be able to use JSON or XML when supplying data. What's
> the
> best practice in a situation like this?

The best practice is whatever works for you...


>    1.   Convert JSON payloads to XML and validate against the XSD.

A danger here is that when there's a validation failure it may be hard
to see why.

>    2.  Convert the XSD to JSON Schema and validate each content-type
> against it's own schema

In general this doesn't make much sense. Here's an excerpt from JSON
Schema:
[[
Note that JSON Schema vocabularies are free to define their own
   extended type system.  This should not be confused with the core
data
   model types defined here.  As an example, "integer" is a reasonable
   type for a vocabulary to define as a value for a keyword, but the
   data model makes no distinction between integers and other numbers.
]]

XML Schema has approx. 40 built-in types and does differentiate between
different sorts of number.

It would be reasonable for W3C to produce an extended JSON Schema
vocabulary for validating according to XSD types, and a way for user-
defined types from an XML Schema to be mapped to a JSON linked data
vocabulary, but there's no Working Group at W3C for that, and I doubt
there's interest at IETF.

>    3.  Convert the XSD to a class-based in language specification and
> do schema validation in code and then handle content negotiation in
> the api middleware

i think what you really want is to construct a data model and then
check the data model. You might do that by serializing as XML (or
building a suitable in-memory structure) and using an XML Schema
validator, since you already have externally-defined Schema documents.
Or you could do it in code, to the extent that you care about the
result.

That way you can accept any mix of data formats that you need.

It's not full validation, but it's likely sufficient for your purposes.

Liam


-- 
Liam Quin, W3C,  http://www.fromoldbooks.org/

Available for XML/Document/Information Architecture/
XSLT/XQuery/Web/CSS/Text Processing work and consulting.


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS