Generate Angular2 forms from Swagger API specification - json

I'm looking for a way to generate a set of Angular2 form templates from a Swagger API definition file. I want a result that will allow me to test my POST/PUT requests, and even use it in my app.
After some research I found this Angular2 form library that takes a JSON schema as input: https://github.com/makinacorpus/angular2-schema-form
So if you know of a Swagger -> JSON Schema converter that will work too.
Cheers!

So if you know of a Swagger -> JSON Schema converter that will work
too.
Swagger 2.0 supports a subset of JSON schema draft 4. This is what swagger's Schema object is. From the docs:
The following properties are taken directly from the JSON Schema
definition and follow the same specifications:
$ref - As a JSON Reference
format (See Data Type Formats for further details)
title
description (GFM syntax can be used for rich text representation)
default (Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object)
multipleOf
...
The following properties are taken from the JSON Schema definition but
their definitions were adjusted to the Swagger Specification.
items
allOf
properties
additionalProperties
It should be a fairly simple exercise to manually extract the schema from your swagger, but I don't know of any automated tool to do this. I think the fact some of the JSON schema properties have been modified by swagger may make auto conversion problematic in certain circumstances.

Related

Use object keys as type in JSON Schema

Say I want to validate a YAML file against a JSON schema in Intellij IDEA. The file's structure would be like:
foo:
command: touch /tmp/a.txt # I know I don't need this but it's an example
bar:
command: echo "Hello World!" > /tmp/a.txt
baz:
command: cat /tmp/a.txt
dependencies:
- foo
- bar
So the property names can be any string, but the dependencies should only be keys/property names of the root object. Ideally I would specify an enum, but this question suggests it's not possible Use object property keys as enum in JSON schema (unless the answer is obsolete).
Still, I have noticed that when you write a schema in Intellij and you add a "required" = [...] it autocompletes the required fields with the property names of the "property" object (even though it doesn't use them to validate, but close enough for my purpose). I have checked out the schema for it http://json-schema.org/draft-07/schema# but haven't been able to understand how it does that.
Is there a way that I can define my schema so Intellij autocompletes based on another properties' keys like it does when you define a schema?
There is nothing in the schema itself that indicates possible values from data. There's actually no requirement that items in the required array also be defined in properties.
This sort of functionality is defined by the IDE only.
IntelliJ IDEA documents the ability to add custom schemas:
Besides schemas from JSON Schema Store, IntelliJ IDEA lets you
configure and use custom schemas from other storages. You can download
the required schema and store it under the project root or specify the
URL of the resource so IntelliJ IDEA can download the schema
automatically.
To configure a custom JSON Schema:
In the Settings/Preferences dialog ⌘,, go to Languages and Frameworks
| Schemas and DTDs | JSON Schema Mappings.
https://www.jetbrains.com/help/idea/json.html#ws_json_schema_add_custom
It also details later how to make the intelesense provide a rich preview:
Using HTML descriptions in JSON schema #
By default, IntelliJ IDEA escapes HTML characters when displaying
documentation for JSON schema definitions in documentation popups. To
get nice looking documentation with rich HTML markup, store the HTML
description in the x-intellij-html-description extension property
instead of description.
https://www.jetbrains.com/help/idea/json.html#ws_json_show_doc_in_html
However,
autocompletes based on another properties' keys
sounds like custom functionality specifically designed for writing JSON Schema. JSON Schema itself cannot reference data dynamically like that (which I assume is what you were thinking).

JSON Schema Validation for PL/SQL or SQL, similar to XSLT for XML Schema Validation

I am looking for the schema validation for JSON structure received during the API Invocation from ORDS. Lets say I have 10 different json tags constructing the JSON and I want to validate this JSON against pre-defined validation to check if any required parameters are missing or to validate the datatype.
I am looking for the functionality similar to XLST which is used for XML schema validation.
Please note this schema validation is required for the JSON using PL/SQL and ORDS(Oracle Rest Database Services).
Technical Components: SQL, PL/SQL, ORDS. We want something which is compatible with the mentioned components.

Swipe button in JSON schema

There is a variable which can be 0 or 1. I would like to set in JSON schema as a swipe button, not as a list. So it could be set to on / off. How could I implement this in schema?
JSON Schema describes validation and hyperlinks (JSON Hyper-Schema). It does not describe user interface elements. You will have to describe your user interface elements with something other than JSON Schema.
There are a couple of tools out there that generate a form based on a JSON Schema, but how they do so is defined by the specific tool in use, not JSON Schema.
There has been discussion about creating an official JSON Schema UI specification, but no such thing exists yet.

How do I validate JSON against XML Schema (XSD) or RelaxNG?

I'm defining the structure of JSON documents. I'd like to know if how to validate the JSON documents against existing XSD, RelaxNG schemas or another standard schema language schema. I know of Jsonix, but I don't see that it uses the regular expressions from XSD or RelaxNG for validation against JSON schema (and I don't think that JSON schema is standardized).
Clarification: We already have existing XML and XSD. We can always go back to XML for validation, but it would be cool if we could validate the JSON directly, and would give us more confidence when we want to use the JSON and not XML.
Update: Here is the specification in question: http://www.web3d.org/specifications/x3d-3.4.xsd note that it doesn't have text nodes in the XML documents.
Preliminary answer (still a work in progress, but you can contribute):
If you want to convert XML schema to JSON schema, try downloading: XSD2OWL stylesheet which converts XML Schema to OWL. I converted my schema to owl like this:
$ xmlsh
$ xslt -f xsd2owl.xsl -cf file.xsd > file.owl
$ exit
Then download owl2jsonschema.js ** NO LICENSE ** and modify it until it until the demo works. The output will be in demo/OUTPUT/schema folder/*.json as separate JSON files.
XSD and RelaxNG are defined against XML, not JSON.
For JSON, see JSON Schema, but realize that it has nowhere near the adoption of XSD, and the latest draft of the specification expired August 3, 2013, casting doubts on the future of the effort.
Update
How do I validate JSON against XML Schema (XSD) or RelaxNG?
You don't.
The question is not "Can I?" but "How?" Say I have total control over
the JSON document.
When the answer to "Can I?" is "No" the question of how does not apply.
Clarification: We already have existing XML and XSD. We can always go
back to XML for validation, but it would be cool if we could validate
the JSON directly, and would give us more confidence when we want to
use the JSON and not XML.
You can validate the JSON directly against a JSON Schema, but not against an XSD. There are no tools that can do that; the standards are substantially different. The need to define standard vocabularies and grammars that is served by XSD and RelaxNG against XML was intended to be met by JSON Schema against JSON.
You're looking for "confidence when we want to use the JSON and not XML" in the wrong place. See reasons for choosing XML vs JSON instead.
As you're probably already aware of the information I'm about to post, this is just for reference.
Jsonix Schema Compiler supports generation of JSON Schema based on the XML Schema.
So with this you can convert you XML Schema into JSON Schema and validate your JSON against this JSON Schema using AJV.
This is still an experimental feature but that's the direction.

Is there a json validation framework in play based on a specified grammar

An automated system is going to feed the application[Play with Scala] with JSON's and the contract of the integration is that there would be no validation required on JSON's since it will be always deemed right. But for testing purposes when we seed the data more often than not we are not able to send the correct JSONs. We would like to validate the JSON's we receive based on a set of grammars. Is there a library that already does this. Or is there a better way to do this?
Example: Grammar for valid Json :
"header"->[String, mandatory],
"footer"->[String],
"someArray"->Array[String, mandatory],
"someArrayObject"->Array[
{
{"key1"->Int, mandatory},
{"key2"->String}
},
mandatory
]
and passing,
{
"header":"headerContent",
"footer":"footerContent",
"someArray":["str1", "str2"],
"someArrayObject"->[
{"key1":4, "key2":"someStringValue"},
{"key1":5, "key2":"someOtherStringValue"}
]
} // would pass
{
"header":"headerContent",
"footer":"footerContent",
"someArray":["str1", "str2"]
} // would notpass since someArrayObject though declared mandatory is not provided in the sample json
I think play-json will satisfy you play-json
In play-json you don't create a validator as it is, but a json transformer which is a validator in itself. The author of the framework wrote a series of blog-posts to show how to work with it: json-transformers
* Haven't noticed you use play) Play has play-json included by default.
You don't have to roll out your own DSLs. This is why we have schemas. Just like using XML schemas to validate your XML docs, you can define a JSON schema to validate your JSON objects. I had a similar requirement when building a RESTful web service using Play. I solved it by using the JSON Schema Validator library.
I have used the JSON Schema draft v3. The library supports draft v3 and draft v4. You can validate your schemas against possible JSON inputs using a web application that uses the same library. The web app is hosted here.
Also there are pretty nice examples that use the draft v4. You can check them out from here.
In Play 2, I have composed an action that takes the schema resource file name as input. This keeps away a lot of JSON validation code from the controller action itself.
#JsonValidate("user-register.json")
public static Result create() {
...
}
This way, all JSON Validation code stays in one place. Pretty neat :)