Convert JSON Schema Format - javascript

I have json schema definition like this:
(Update: basically its draft 03 format: http://json-schema.org/draft-03/schema#)
{
"$schema": "http://json-schema.org/draft-03/schema",
"product": {
"name": {
"required": true,
"type": "string"
},
"description": {
"required": true,
"type": "string"
}
},
"type": "object"
}
But I need it in this format(standard json schema structure), which is draft 04 format(http://json-schema.org/draft-04/schema#)
{
"type": "object",
"properties": {
"product": {
"type": "object",
"required": [
"name",
"description"
],
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
}
}
}
},
"required": [
"product"
]
}
Is there any converter to convert the above format to this one? I just don't want to manually do it which might be error prone.

I haven't used it, so I can't vouch for it personally, but there is conversion tool.
https://github.com/geraintluff/json-schema-compatibility

Related

Validating an object in parts using json-schema

I have a complex object that I need to validate in parts (each part is validated by a different party). I guess I can create a schema file per each part, but I really want to be able to use one file.
For example, my schema could be the following, and I want to be able to validate an object against obj1.
I'm trying to figure out a standard way of defining such a situation and what I can expect from implementations. Not looking necessarily for a specific library. But if some libraries support this and some don't, I'm interested in Javascript ones.
{
"required": [
"prop1",
"another",
"obj1",
"obj2"
],
"properties": {
"prop1": {
"type": "integer"
},
"another": {
"type": "string"
},
"obj1": {
"$ref": "#/$defs/obj1"
},
"obj2": {
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"type": "string"
}
}
}
},
"$defs": {
"obj1": {
"type": "object",
"required": [
"sub1",
"sub2",
"a_number"
],
"properties": {
"sub1": {
"type": "string"
},
"sub2": {
"type": "string"
},
"a_number": {
"type": "integer"
}
}
}
}
}

Is it possible to extract class/interface functions with ts-json-schema-generator?

I am working on a Builder for a typescript sdk library and I am trying to extract all the sdk classes with their respective functions into a json that the Builder can use.
I am trying to accomplish this:
class Form {
propA: string
propB: string
funcA(value: boolean, text: string) {
...
}
}
to
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Form": {
"type": "object",
"properties": {
"propA": {
"type": "string"
},
"propB": {
"type": "string"
},
"NamedParameters<typeof funcA>": {
"type": "object",
"properties": {
"value": {
"type": "boolean"
},
"text": {
"type": "string"
}
},
"required": [
"value",
"text"
],
"additionalProperties": false
}
},
"additionalProperties": false
}
}
}
But there seems to be no way to accomplish this. Does anyone know an alternative or a solution?

Angular Schema Form - Require Field if Integer Field is Not Null

I am trying to create a form using the Angular JSON Schema Form. I want one field (dropdown1) to be required when another field (number1) is populated. I am able to get the following form + schema working in the SchemaForm.io Sandbox, but when I put it into my site, the dropdown field is missing.
Here is my schema:
{
"type": "object",
"properties": {
"isChecked": {
"title": "checked?",
"type": "boolean"
},
"text1": {
"title": "text1",
"type": "number",
"minimum": 0,
"maximum": 100,
"condition": "model.isChecked"
},
"number1": {
"title": "number1",
"type": "number",
"minimum": 0,
"condition": "model.isChecked"
},
"dropdown1": {
"title": "",
"type": "string",
"enum": ["Days", "Months", "Years"],
"condition": "model.isChecked"
},
"comment": {"type": "string", "Title": "Comment"}
}
}
Here is my form:
[
"isChecked",
{
"key": "text1",
"type": "decimal",
"validationMessages": {
"maximum": "text1 must be 100 or lower.",
"minimum": "text1 must be 0 or higher.",
"required": "This field is required"
},
"required": false
},
{
"key": "number1",
"type": "number",
"validationMessages": {
"minimum": "number1 must be 0 or higher."
},
"required": false
},
{
"key": "dropdown1",
"required": false,
"condition": "model.number1 === null"
},
{
"key": "dropdown1",
"required": true,
"condition": "model.number1 > 0",
"validationMessages": {
"required": "dropdown1 is required."
}
},
{
"key": "comment",
"type": "textarea"
}
]
You can use the "dependencies" property (https://json-schema.org/understanding-json-schema/reference/object.html#property-dependencies). The example requires billing_address to be present when a credit_card is provided:
{
"type": "object",
"properties": {
"credit_card": {
"type": "number"
},
"billing_address": {
"type": "string"
}
},
"dependencies": {
"credit_card": [
"billing_address"
]
}
}
This implementation supports "dependencies":
https://github.com/dashjoin/json-schema-form
You can check out the online example here:
https://dashjoin.github.io/#/example/dependencies
Here is the solution I found:
"condition": {
"functionBody": "return model.number1 === undefined && model.isChecked"
}

Form multiple groups for a complex object in uiSchema

Description
I'm using library react-json-schema to display JSON schema as a form.
I have a requirement, where the form is very large, hence I'm using uiSchema groups to form the tabs. But one of my attributes, which is an object, contains many objects. I want to spread these child objects further into the tabs.
I used the code suggested in #784 . I went through GroupedSchema code and it checks only in properties of schema, if the given field is present or not. It doesn't go deep down inside a nested object.
Steps to Reproduce
1.A dummy Schema:
{
"A": {
"type": "string",
"title": "A"
},
"B": {
"type": "string",
"title": "B"
},
"C": {
"type": "string",
"title": "C"
},
"D": {
"type": "object",
"properties": {
"DA": {
"type": "object",
"properties": {
"DAA": {
"type": "string",
"title": "DAA"
},
"DAB": {
"type": "string",
"title": "DAB"
}
}
},
"DB": {
"type": "object",
"properties": {
"DBA": {
"type": "string",
"title": "DBA"
},
"DBB": {
"type": "string",
"title": "DBB"
}
}
},
"DC": {
"type": "object",
"properties": {
"DCA": {
"type": "string",
"title": "DCA"
},
"DCB": {
"type": "string",
"title": "DCB"
}
}
}
}
}
}
Expected behavior
I'm trying to get tabs for A,B,C,DA,DB,DC
Here is the link to codesandbox

AJV is not validating my schema

I am facing problem regarding AJV Schema Validator.
I have following schema
{
"$id": "create-offer.json#",
"body": {
"type": "object",
"properties": {
"statusCode": {
"type": "number"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"status": {
"type": "string"
},
"type": {
"type": "string"
},
"routePlanId": {
"type": "string"
},
"currencyId": {
"type": "string"
},
"autoRateUpdateActive": {
"type": "boolean"
}
}
}
}
And my response is :
{ statusCode: 2006,
statusPhrase: 'Error: ORA-00001: unique constraint (SPHERE_D1.CHECK_UNIQUE_RATE_NAME) violated\nORA-06512: at "SPHERE_D1.PKG_RATE_TABLES_V2", line 102\nORA-06512: at "SPHERE_D1.PKG_RATE_TABLES_V2", line 54\nORA-06512: at line 1' }
Using the following code to validate :
let valid = ajv.validate(schema, res);
var detailedErrorMsg = "\n" + ajv.errorsText(ajv.errors, { separator: "\n" }) + "\n";
console.log(detailedErrorMsg);
AJV should return error as schema and response are different, but AJV is returning 'no errors'.
Is there any problem with the code ?
This is resolved by adding required feilds in schema definition.
{
"$id": "create-offer.json#",
"description": "",
"title": "",
"type": "object",
"required": [
/*mention objects which should be requird*/
]
}

Categories

Resources