Problem validating schema in postman using "floats" - json

I am triying to validate a schema with postman.
I created the following schema using the website: https://www.liquid-technologies.com/online-json-to-schema-converter
Here's the result:
const schema = {
"type": "object",
"properties": {
"system_id": {"type": "string"},
"start": {"type": "string"},
"end": {"type": "string"},
"period": {"type": "string" },
"unit": {"type": "string"},
"values": {"type": "array",
"items": [
{
"type": "object",
"properties": {
"timestamp": {"type": "string"},
"battery_charge": {"type": "float" },
"battery_discharge": {"type": "number"},
"grid_export": {"type": "number"},
"grid_import": {"type": "number"},
"home_consumption": {"type": "number"},
"solar": {"type": "number"},
"battery_charge_state": {"type": "number"}
},
"required": [
"timestamp",
"battery_charge",
"battery_discharge",
"grid_export",
"grid_import",
"home_consumption",
"solar",
"battery_charge_state"
]
}
]
},
"totals": {
"type": "object",
"properties": {
"battery_charge": {"type": "number"},
"battery_discharge": {"type": "number"},
"grid_export": {"type": "number"},
"grid_import": {"type": "number"},
"home_consumption": {"type": "number"},
"solar": {"type": "number"}
},
"required": [
"battery_charge",
"battery_discharge",
"grid_export",
"grid_import",
"home_consumption",
"solar"
]
}
},
"required": [
"system_id",
"start",
"end",
"period",
"unit",
"values",
"totals"
]
}
pm.test("Schema validation", () => {
pm.response.to.have.jsonSchema(schema);
});
But the problem is that appears some "type: number" and it should be "float". If I change this number to float, Postman displays the following error message:
Schema validation | Error: schema is invalid: data.properties['values'].items should be object,boolean, data.properties['values'].items[0].properties['battery_charge'].type should be equal to one of the allowed values, data.properties['values'].items[0].properties['battery_charge'].type should be array, data.properties['values'].items[0].properties['battery_charge'].type should match some schema in anyOf, data.properties['values'].items should match some schema in anyOf
Json Response:
{
"system_id": "C18208",
"start": "2022-02-06T00:00:00+00:00",
"end": "2022-02-06T23:59:00+00:00",
"period": "minute",
"unit": "kWh",
"values": [{
"timestamp": "2022-02-06T00:00:00+00:00",
"battery_charge": 0.0,
"battery_discharge": 0.0,
"grid_export": 0.0,
"grid_import": 0.0,
"home_consumption": 0.0,
"solar": 0.0,
"battery_charge_state": 100.0
}],
"totals": {
"battery_charge": 9.3,
"battery_discharge": 4.8,
"grid_export": 4.5,
"grid_import": 31.9,
"home_consumption": 32.1,
"solar": 33.3
}
}
Any help please?

With json, there are only 6 data types string,number,array,object,null,boolean, there is no such data type "float".
Fix:
"battery_charge": {"type": "float" } --> "battery_charge": {"type": "number" }

Related

Error: Undefined data set name: "source_1" in Vega-Lite interactive charts

I am making an interactive dashboard using Vega-Lite. The end graph has to look like this:
Vega-Lite Dashboard
The code I have so far works perfectly fine for the bar chart and map, but when I draw the histogram it gives following error:
Undefined data set name: "source_1"
This is the code I have so far:
"$schema": "https://vega.github.io/schema/vega-lite/v5.json"
"title": {
"text": "Exploring Irish",
"anchor": "middle",
"fontSize": 20,
"offset": 20,
"color": "brown"
},
"vconcat":[
{ "hconcat": [
{
"width": 500,
"height": 700,
"projection": {
"type": "conicConformal"
},
"layer": [
{
"data": {
"url": "https://gist.githubusercontent.com/carsonfarmer/9791524/raw/b27ca0d78d46a84664fe7ef709eed4f7621f7a25/irish-counties-segmentized.topojson",
"format": {
"type": "topojson",
"feature": "counties"
}
},
"transform": [{
"lookup": "id",
"from": {
"data": {"url": "https://raw.githubusercontent.com/colmr/vis_class/master/FakeAttractionDetails.csv"},
"key": "County",
"fields": ["Population"]
}
}],
"mark": {
"type": "geoshape",
"stroke": "white",
"fill":"#ccc"
}
},
{
"data": {
"url": "https://raw.githubusercontent.com/colmr/vis_class/master/FakeAttractionDetails.csv"
},
"mark": "circle",
"params": [{
"name": "Attrac",
"select": {"type": "point", "fields": ["Type"]},
"bind": "legend"
}],
"encoding": {
"longitude": {
"field": "Longitude",
"type": "quantitative"
},
"latitude": {
"field": "Latitude",
"type": "quantitative"
},
"color":{"field":"Type", "type":"nominal", "scale": {"range": ["#E69F00", "#0072B2", "#CC79A7","#009E73","#56B4E9"]}},
"size": {"value": 40},
"opacity": {"condition": {"param": "Attrac", "value": 1},
"value": 0},
"tooltip": [
{"field": "Name", "type": "nominal", "title": "Accommodation"},
{"field": "Type", "type": "nominal", "title": "Property Type"},
{"field": "Telephone", "type": "nominal", "title": "Contact"}
],
"href": {"field": "Url", "type": "nominal"}
}
}
]
},
{
"data": {
"url": "https://raw.githubusercontent.com/colmr/vis_class/master/FakeAttractionDetails.csv"
},
"width": 335,
"height": 700,
"mark": "bar",
"params": [{
"name": "Attrac",
"select": {"type": "point", "fields": ["Type"]},
"bind": "legend"
},
{ "name": "Attrac",
"select": {"type": "point", "encodings": ["y"]}
}
],
"encoding": {
"y": {
"field": "AddressRegion",
"type": "nominal",
"sort": {
"op": "count",
"field": "Type",
"order": "descending"
},
"axis":{"title":null, "labelFontSize": 15}
},
"x": {
"field": "Type",
"type": "nominal",
"aggregate":"count",
"axis":{"title":"Total Accommodations", "titleFontSize":15}
},
"color":{"field":"Type", "type":"nominal",
"scale": {"range": ["#E69F00", "#0072B2", "#CC79A7","#009E73","#56B4E9"]}
},
"opacity": {"condition": {"param": "Attrac", "value": 1},
"value": 0.05},
"order": {"aggregate": "count", "field": "Type", "type": "nominal", "sort": "descending"}
}
}
]
},
{
"data": {"url": "https://raw.githubusercontent.com/colmr/vis_class/master/FakeAttractionDetails.csv"},
"width": 950,
"height": 45,
"mark": "bar",
"params": [{
"name": "Attrac",
"select": {"type": "point", "fields": ["Type"]},
"bind": "legend"},
{ "name": "Attrac",
"select": {"type": "interval", "encodings": ["x"]}
}],
"encoding": {
"x": {
"field": "Popularity",
"bin": true,
"type": "quantitative"
},
"y": {"aggregate": "count"},
"color":{"field":"Type", "type":"nominal",
"scale": {"range": ["#E69F00", "#0072B2", "#CC79A7","#009E73","#56B4E9"]}},
"opacity": {"condition": {"param": "Attrac", "value": 1},
"value": 0.02}
}
}
],
"config": {
"legend": {
"orient":"top-left", "labelFontSize":15, "titleFontSize":15
}, "tick": {"thickness": 1.5, "bandSize": 18}
}
}
I tried making the histogram with another csv file and that worked fine. Any idea what is wrong with this dataset or this code?
It looks like there's some issue with specifying the same data URL in multiple places in the chart. If you move the data specification to the top level, it appears to work:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"url": "https://raw.githubusercontent.com/colmr/vis_class/master/FakeAttractionDetails.csv"
},
...

How do I represent polymorphic objects in JSON schema?

I'm trying to create a schema for a JSON object that varies its schema depending on the value of one of its properties: type.
Like this:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["INT", "PERCENT"]
}
},
"required": ["type"],
"allOf": [
{
"if": {
"properties": {"type": {"const": "INT"}}
},
"then": {
"properties": {
"value": {"type": "number", "multipleOf": 1}
},
"required": ["value"],
"additionalProperties": false
}
},
{
"if": {
"properties": {"type": {"const": "PERCENT"}}
},
"then": {
"properties": {
"value": {"type": "number"},
"min": {"type": "number"},
"max": {"type": "number"}
},
"required": ["value", "min", "max"],
"additionalProperties": false
}
}
]
}
But I'm getting all sorts of misbehaviour from the various validators I'm trying.
Some examples with problems noted after the //:
{
"type": "PERCENT", // property type has not been defined(?!)
"value": 0.0,
"min": 10,
"max": 25
}
{
"type": "INT", // no errors allowed, and value
"value": 0.1, // should've been flagged as not multiple of 1
"min": 10, // should've been flagged as disallowed additional property
"max": 25 // same as above
}
Here's the validator I'm trying
Thanks in advance for your help!
It seems that you've figured out that the problem is that addtionalProperties only considers the properties defined in the sub-schema it's defined in. That's why you have to include the "type" property in your then. You have a few options that are an improvement over the solution you posted that you weren't happy with.
Option 1: unevaluatedProperties
Draft 2019-09 introduced the unevaluatedProperties keyword. If you use this at the top level of your schema and don't use addtionalProperties anywhere, you will get the behavior you expected from additionalProperties. I see you are using draft-07, so you have to upgrade in order to use this solution.
Option 2: Boolean schemas
The solution you came up with is to re-define the "type" property in the then schemas. additionalProperties just needs "type" to be declared, you don't need to include the schema for it again. You can use true or the empty schema {} instead.
Option 3: propertyNames
Instead of additionalProperties you can use propertyNames. This allows you to declare a list of which property names are allowed in the object rather than hacking additionalProperties with true or {} to figure out the allowed property names the way you want it to.
well I found a way to get what I wanted, but it doesn't mean I like it. Having to redefine "type" doesn't seem right to me.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["INT", "PERCENT"]
}
},
"required": ["type"],
"allOf": [
{
"if": {
"properties": {"type": {"const": "INT"}}
},
"then": {
"properties": {
"value": {"type": "number", "multipleOf": 1},
"type": {"type": "string"}
},
"required": ["value", "type"],
"additionalProperties": false
}
},
{
"if": {
"properties": {"type": {"const": "PERCENT"}}
},
"then": {
"properties": {
"value": {"type": "number"},
"min": {"type": "number"},
"max": {"type": "number"},
"type": {"type": "string"}
},
"required": ["value", "min", "max", "type"],
"additionalProperties": false
}
}
]
}
tests...
This validates:
{
"type": "PERCENT",
"value": 0,
"min": 10,
"max": 20
}
so does this:
{
"type": "INT",
"value": 0
}
this errors in the way I want:
{
"type": "INT",
"value": 0.1, // not a multiple of 1
"min": 4, // not expected
"max": 5 // not expected
}
and so does this:
{
"type": "PERCENT",
"value": 0.1,
"max": 5 // min is a required property
}

AWS SageMaker SparkML Schema Eroor: member.environment' failed to satisfy constraint

I am deploying a model onto AWS via Sagemaker:
I set up my JSON schema as follow:
import json
schema = {
"input": [
{
"name": "V1",
"type": "double"
},
{
"name": "V2",
"type": "double"
},
{
"name": "V3",
"type": "double"
},
{
"name": "V4",
"type": "double"
},
{
"name": "V5",
"type": "double"
},
{
"name": "V6",
"type": "double"
},
{
"name": "V7",
"type": "double"
},
{
"name": "V8",
"type": "double"
},
{
"name": "V9",
"type": "double"
},
{
"name": "V10",
"type": "double"
},
{
"name": "V11",
"type": "double"
},
{
"name": "V12",
"type": "double"
},
{
"name": "V13",
"type": "double"
},
{
"name": "V14",
"type": "double"
},
{
"name": "V15",
"type": "double"
},
{
"name": "V16",
"type": "double"
},
{
"name": "V17",
"type": "double"
},
{
"name": "V18",
"type": "double"
},
{
"name": "V19",
"type": "double"
},
{
"name": "V20",
"type": "double"
},
{
"name": "V21",
"type": "double"
},
{
"name": "V22",
"type": "double"
},
{
"name": "V23",
"type": "double"
},
{
"name": "V24",
"type": "double"
},
{
"name": "V25",
"type": "double"
},
{
"name": "V26",
"type": "double"
},
{
"name": "V27",
"type": "double"
},
{
"name": "V28",
"type": "double"
},
{
"name": "Amount",
"type": "double"
},
],
"output":
{
"name": "features",
"type": "double",
"struct": "vector"
}
}
schema_json = json.dumps(schema)
print(schema_json)
And deployed as:
from sagemaker.model import Model
from sagemaker.pipeline import PipelineModel
from sagemaker.sparkml.model import SparkMLModel
sparkml_data = 's3://{}/{}/{}'.format(s3_model_bucket, s3_model_key_prefix, 'model.tar.gz')
# passing the schema defined above by using an environment variable that sagemaker-sparkml-serving understands
sparkml_model = SparkMLModel(model_data=sparkml_data, env={'SAGEMAKER_SPARKML_SCHEMA' : schema_json})
xgb_model = Model(model_data=xgb_model.model_data, image=training_image)
model_name = 'inference-pipeline-' + timestamp_prefix
sm_model = PipelineModel(name=model_name, role=role, models=[sparkml_model, xgb_model])
endpoint_name = 'inference-pipeline-ep-' + timestamp_prefix
sm_model.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge', endpoint_name=endpoint_name)
I got the error as below:
ClientError: An error occurred (ValidationException) when calling the CreateModel operation: 1 validation error detected: Value '{SAGEMAKER_SPARKML_SCHEMA={"input": [{"type": "double", "name": "V1"}, {"type": "double", "name": "V2"}, {"type": "double", "name": "V3"}, {"type": "double", "name": "V4"}, {"type": "double", "name": "V5"}, {"type": "double", "name": "V6"}, {"type": "double", "name": "V7"}, {"type": "double", "name": "V8"}, {"type": "double", "name": "V9"}, {"type": "double", "name": "V10"}, {"type": "double", "name": "V11"}, {"type": "double", "name": "V12"}, {"type": "double", "name": "V13"}, {"type": "double", "name": "V14"}, {"type": "double", "name": "V15"}, {"type": "double", "name": "V16"}, {"type": "double", "name": "V17"}, {"type": "double", "name": "V18"}, {"type": "double", "name": "V19"}, {"type": "double", "name": "V20"}, {"type": "double", "name": "V21"}, {"type": "double", "name": "V22"}, {"type": "double", "name": "V23"}, {"type": "double", "name": "V24"}, {"type": "double", "name": "V25"}, {"type": "double", "name": "V26"}, {"type": "double", "name": "V27"}, {"type": "double", "name": "V28"}, {"type": "double", "name": "Amount"}], "output": {"type": "double", "name": "features", "struct": "vector"}}}' at 'containers.1**.member.environment' failed to satisfy constraint: Map value must satisfy constraint: [Member must have length less than or equal to 1024,** Member must have length greater than or equal to 0, Member must satisfy regular expression pattern: [\S\s]*]
I try to reduce my features to 20 and it able to deploy. Just wondering how can I Pass the schema with 29 attributes?
I do not think the environment length of 1024 limit will be increased in a short time. To work around this, you could try to rebuild the spark ml container with the SAGEMAKER_SPARKML_SCHEMA env var:
https://github.com/aws/sagemaker-sparkml-serving-container/blob/master/README.md#running-the-image-locally

How to make a JSON Schema for a JSON file in Oxygen

I have a Json file and I need to write a Scheme for it in Oxygen.
"characters": [
{
"house":"Gryffindor",
"orderOfThePhoenix":false,
"name":"Cuthbert Binns",
"bloodStatus":"unknown",
"deathEater":false,
"dumbledoresArmy":false,
"school":"Hogwarts School of Witchcraft and Wizardry",
"role":"Professor, History of Magic",
"__v":0,
"ministryOfMagic":false,
"_id":"5a0fa67dae5bc100213c2333",
"species":"ghost"
}
],
"spells": [
{
"spell":"Aberto",
"effect":"opens objects",
"_id":"5b74ebd5fb6fc0739646754c",
"type":"Charm"
}
],
"houses": [
{
"values": [
"courage",
"bravery",
"nerve",
"chivalry"
],
"headOfHouse":"Minerva McGonagall",
"mascot":"lion",
"name":"Gryffindor",
"houseGhost":"Nearly Headless Nick",
"founder":"Goderic Gryffindor",
"colors": [
"scarlet",
"gold"
],
"school":"Hogwarts School of Witchcraft and Wizardry",
"__v":0,
"members": [
"5a0fa648ae5bc100213c2332",
"5a0fa67dae5bc100213c2333",
"5a0fa7dcae5bc100213c2338",
"5a123f130f5ae10021650dcc"
],
"_id":"5a05e2b252f721a3cf2ea33f"
},
For sure the current JSON file is much bigger. If someone could send related links it would help too, or some kind of tutorials.
Could you please help me with creating a schema for it?
If you want to create a JSON Schema, the best way to start is to check the "json-schema.org" tutorials. You can find them here:
https://json-schema.org/learn/getting-started-step-by-step.html
https://json-schema.org/understanding-json-schema/
In the next version of Oxygen there will be support to create a JSON Schema based on a JSON instance or on an XSD, but you will need to check the created schema and customize it for your needs.
For example, for the instance you provided the schema can look something like this:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"characters": {"$ref": "#/definitions/characters_type"},
"spells": {"$ref": "#/definitions/spells_type"},
"houses": {"$ref": "#/definitions/houses_type"}
},
"definitions": {
"characters_type": {
"type": "array",
"minItems": 0,
"items": {
"type": "object",
"properties": {
"house": {"type": "string"},
"orderOfThePhoenix": {"type": "boolean"},
"name": {"type": "string"},
"bloodStatus": {"type": "string"},
"deathEater": {"type": "boolean"},
"dumbledoresArmy": {"type": "boolean"},
"school": {"type": "string"},
"role": {"type": "string"},
"__v": {"type": "number"},
"ministryOfMagic": {"type": "boolean"},
"_id": {"type": "string"},
"species": {"type": "string"}
},
"required": [
"role",
"bloodStatus",
"school",
"species",
"deathEater",
"dumbledoresArmy",
"__v",
"name",
"ministryOfMagic",
"_id",
"orderOfThePhoenix",
"house"
]
}
},
"spells_type": {
"type": "array",
"minItems": 0,
"items": {
"type": "object",
"properties": {
"spell": {"type": "string"},
"effect": {"type": "string"},
"_id": {"type": "string"},
"type": {"type": "string"}
},
"required": [
"spell",
"effect",
"_id",
"type"
]
}
},
"values_type": {
"type": "array",
"minItems": 0,
"items": {"type": "string"}
},
"houses_type": {
"type": "array",
"minItems": 0,
"items": {
"type": "object",
"properties": {
"values": {"$ref": "#/definitions/values_type"},
"headOfHouse": {"type": "string"},
"mascot": {"type": "string"},
"name": {"type": "string"},
"houseGhost": {"type": "string"},
"founder": {"type": "string"},
"colors": {"$ref": "#/definitions/values_type"},
"school": {"type": "string"},
"__v": {"type": "number"},
"members": {"$ref": "#/definitions/values_type"},
"_id": {"type": "string"}
},
"required": [
"headOfHouse",
"houseGhost",
"mascot",
"school",
"founder",
"values",
"__v",
"members",
"name",
"_id",
"colors"
]
}
}
}
}
Best Regards,
Octavian

Filter by selection from referenced table

I have a single data source within datasets that I use in two concatenated charts. Now when I click on a bar in the left chart I would like to filter the right chart. See animated gif:
But is doesn't.
Click here to open in the vega-editor.
Or check the spec here::
{
"config": {"view": {"width": 400, "height": 300}},
"hconcat": [
{
"width": 100,
"selection": {"SELECT": {"type": "single", "resolve": "global"}},
"data": {"name": "table_data"},
"mark": "bar",
"encoding": {
"x": {"type": "nominal", "field": "Major_Genre"},
"y": {"aggregate": "count", "type": "quantitative"},
"color": {"type": "nominal", "field": "Major_Genre"},
"fillOpacity": {
"condition": {"selection": "SELECT", "value": 1},
"value": 0.3
}
}
},
{
"width": 100,
"data": {"name": "table_data"},
"transform": [{"filter": {"selection": "SELECT"}}],
"mark": "bar",
"encoding": {
"x": {"type": "nominal", "field": "Major_Genre"},
"y": {"aggregate": "count", "type": "quantitative"},
"color": {"type": "nominal", "field": "Major_Genre"}
}
}
],
"datasets": {
"table_data": [
{
"Title": "Cidade de Deus",
"Major_Genre": "Drama",
"Country_Origin": "Brazil"
},
{
"Title": "Chocolate: Deep Dark Secrets",
"Major_Genre": "Thriller/Suspense",
"Country_Origin": "India"
},
{"Title": "Fiza", "Major_Genre": "Drama", "Country_Origin": "India"},
{
"Title": "First Love, Last Rites",
"Major_Genre": "Drama",
"Country_Origin": "United States"
},
{
"Title": "Foolish",
"Major_Genre": "Comedy",
"Country_Origin": "United States"
},
{
"Title": "I Married a Strange Person",
"Major_Genre": "Comedy",
"Country_Origin": "United States"
}
]
}
}
When your selections refer to groups of points rather than single points, you need to provide an explicit fields or encodings argument to specify what points are included in the selection.
In this case, the chart will behave as expected if you specify your selection in one of the following ways:
"selection": {"SELECT": {"type": "single", "fields": ["Major_Genre"], "resolve": "global"}}
"selection": {"SELECT": {"type": "single", "encodings": ["color"], "resolve": "global"}}
"selection": {"SELECT": {"type": "single", "encodings": ["x"], "resolve": "global"}}
Here's the working chart in action: (vega editor link).