I've seen this other question but it's not quite the same, and I feel like my issue is simpler, but just isn't working.
My data would look like this:
[
{ "loc": "a value 1", "toll" : null, "message" : "message is sometimes null"},
{ "loc": "a value 2", "toll" : "toll is sometimes null", "message" : null}
]
I'm wanting to use AJV for JSON validation in a Node.js project and I've tried several schemas to try to describe my data, but I always get this as the error:
[ { keyword: 'type',
dataPath: '',
schemaPath: '#/type',
params: { type: 'array' },
message: 'should be array' } ]
The schema I've tried looks like this:
{
"type": "array",
"items": {
"type": "object",
"properties": {
"loc": {
"type": "string"
},
"toll": {
"type": "string"
},
"message": {
"type": "string"
}
},
"required": [
"loc"
]
}
}
I've also tried to generate the schema using this online tool but that also doesn't work, and to verify that that should output the correct result, I've tried validating that output against jsonschemavalidator.net, but that also gives me a similar error:
Found 1 error(s)
Message:
Invalid type. Expected Array but got Object.
Schema path:
#/type
You have defined your schema correctly, except that it doesn't match the data you say you are validating. If you change the property names to match the schema, you still have one issue. If you want to allow "toll" and "message" to be null, you can do the following.
{
"type": "array",
"items": {
"type": "object",
"properties": {
"loc": {
"type": "string"
},
"toll": {
"type": ["string", "null"]
},
"message": {
"type": ["string", "null"]
}
},
"required": [
"loc"
]
}
}
However, that isn't related to the error message you are getting. That message means that data you are validating is not an array. The example data you posted should not result in this error. Are you running the validator on some data other than what is posted in the question?
Related
I need to validate messages on a brokers side.
I run cp-server (merely ran cp-all-in-one compose file).
created a topic
set confluent.value.schema.validation to true
registered a schema (JSON)
produced a message
It always fails. Why validation fails? Should I change configuration?
Schema:
{
"$id": "http://example.com/models/data-item-definition.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "test data item 1",
"properties": {
"array_val": {
"items": {
"type": "string"
},
"type": [
"array",
"null"
]
},
"int_val": {
"type": "integer"
},
"string_val": {
"type": "string"
}
},
"required": [
"string_val",
"int_val"
],
"title": "data item",
"type": "object"
}
Message:
{
"string_val": "text",
"int_val": 10,
"array_val": ["one", "two", "three"]
}
An issue is that if confluent.value.schema.validation is true then to produce a message we need to send a magic byte and a schema ID at the beginning.
See Wire Format.
I'm trying to create a Pub/Sub Schema Topic on AVRO respecting the indications on the documentation with "default" : null indication.
I declared my optional field this way :
{
"name": "myField",
"type": ["null","string"],
"default": null
}
The error I get :
Incorrect token in the stream. Expected: Object start, found String
Do you have any idea on how to solve this ?
Not sure if this is exactly what you would like, but I would probably try something like this:
{
"type": "record",
"name": "SomeName",
"fields": [
{
"name": "myField",
"type": ["string", "null"]
}
]
}
as defined in Apache Avro specification and described in PubSub Schema creation
#Snowfire, you have mentioned in the comment that you choose to continue with schema-less topic.
You can follow this document to create Pub/Sub Schema.
To create a schema of Avro type you can try this:
{
"type": "record",
"name": "Avro",
"fields": [
{
"name": "testname",
"type": ["null", "string"],
"default": null
},
{
"name": "testId",
"type": ["null", "int"],
"default": null
}
]
}
I tested below message against the schema and it works.
{
"testname": {
"string": "Jack"
},
"testId": {
"int": 101
}
}
I'm trying to validate a JSON within its associated schema.
I'm validating using the following website : https://www.jsonschemavalidator.net/
My test data looks like this
[
{
"testString": "string value",
"testInt": 1
},
{
"testString": "another string value",
"testInt": 2
}
]
And the schema is defined as follow
{
"type": "array",
"items": [
{
"type": "object",
"properties": {
"testString": {
"type": "string"
},
"testInt": {
"type": "integer"
}
},
"required": [
"testString",
"testInt"
],
"additionalProperties": false
}
]
}
It works well for the given example, however if I add an unexpected attribute into the second element it's not detected as an error.
Otherwise it is if I add it into the first element
So this works (detected as wrong JSON)
[
{
"testString": "string value",
"testInt": 1,
"unexpectedAttribute": "unexpected value"
},
{
"testString": "another string value",
"testInt": 2
}
]
But this doesn't works (detected as valid JSON)
[
{
"testString": "string value",
"testInt": 1
},
{
"testString": "another string value",
"testInt": 2,
"unexpectedAttribute": "unexpected value"
}
]
I also tried with this website but I have the same result, so I think that I may have forgotten something into my schema.
Does anybody already faced the same issue ?
you schema has a bug, remove extra [] from items. When you are using [], you have to add rules for each element of array. In you cases only the first element has rules, anothers not. You can change the second item of your items array any way, it will not generate any error, since it is not validated at all.
{
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"testString": {
"type": "string"
},
"testInt": {
"type": "integer"
}
},
"required": [
"testString",
"testInt"
]
}
}
I have the following JSON data that I would like to validate.
[
{ "fieldType": "oneThing" },
{ "fieldType": "anotherThing" },
{ "fieldType": "oneThing" }
]
And my current (non working) schema is:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"oneOf": [
{ "$ref": "#/definitions/oneThing" },
{ "$ref": "#/definitions/anotherThing" }
]
},
"definitions": {
"oneThing": {
"type": "object",
"properties": {
"fieldType": {
"type": "string",
"pattern": "oneThing"
}
},
"required": [
"fieldType"
]
},
"anotherThing": {
"type": "object",
"properties": {
"fieldType": {
"type": "string",
"pattern": "anotherThing"
}
},
"required": [
"fieldType"
]
}
}
}
I'm getting the following error but I fail to see what I'm doing wrong.
[] Object value found, but an array is required
More context: I'm generating a dynamic HTML form based on a JSON configuration. The HTML form will have a specific set of valid field types and the same field type may exist multiple times in the config, thus oneThing appearing more than once in the above sample json.
As it turns out, this had nothing to do with my JSON schema but with how I was calling the library that was parsing the schema.
I'm using https://github.com/justinrainbow/json-schema and was passing the wrong data type to the class. Duh!
I have the JSON schemas below:
Schema A:
{
"$schema": "http://json-schema.org/draft-04/hyper-schema#",
"title": "A",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"entityBId": {
"type": "string"
}
},
"required": [
"name",
"entityBId"
],
"links": [
{
"rel": "B",
"href": "myapi/EntityB?id={entityBId}"
}
]
}
Schema B :
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "B",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
I'm trying to figure out if there is a way to run something like a integrity check based in a JSON Schema with external links/references.
For instance: When I receive an Object A with a entityBId = 1, I'd like to fetch this entity B running a GET in the endpoint declared in the links href and check if there is a valid object from the received id.
It will run like a deep validation and can be useful in a scenario without a defined DB schema.
As suggested by Raj Kamal, I made my own "link validation".
There is a function that looks for link attribute on schemas and validate the externals references directly on database and throw an exception if a valid reference is not found.