Key/value pairs pylance issue - pylance

After typing the following script: my_posts = [{"title": "title of post 1", "content": "content of post 1", "id": 1},
{"favorite foods", "content": "I like pizza", "id": 2}]
Key/value pairs are not allowed within a set Pylance is the error message I am receiving with "I like pizza" and 2.
Here are the libraries I have imported:
from typing import Optional
from fastapi import FastAPI
from fastapi.params import Body
from pydantic import BaseModel
I would appreciate any insight regarding this issue.

The first entry in your second dictionary ("favorite_foods") is just a string, not a key value pair. This makes pylance (and python for that mattet) treat it as a set, not a dictionary, which is why you’re getting the error you’re getting. I'm guessing you meant "title": "favorite_foods".

Related

JSON-LD: What is the correct syntax for the Report schema type?

I've used several JSON-LD formatting tools (Example 1, Example 2, Example 3), but none are so specific as to list the schema type Report, and its property reportNumber. The schema type is documented here, but without examples.
This page has been a helpful reference, but I still have uncertainty.
So I would like to know if the following syntax is correct, and what to change if it's not:
<script type="application/ld+json">
{
"#context": "https://schema.org",
"#type": "Report",
"reportNumber": "1234",
"headline": "Report Headline",
"description": "Report Description",
"image": "img.jpg",
"author": {
"#type": "Organization",
"name": "Org. Name",
"url": "example.com"
},
"publisher": {
"#type": "Organization",
"name": "Org. Name",
"logo": {
"#type": "ImageObject",
"url": "logo.svg"
}
},
"datePublished": "2021-11-24",
"dateModified": "2021-11-24"
}
</script>
I just swapped the type Article for Report. It appears to me that Report is a subset of the Article type, making properties like headline and description still valid, but in addition, making the property reportNumber valid. I'm new to JSON-LD.
It looks like you are doing it correctly. In JSON-LD you can use any of the properties of a class in any of its subclasses.
In order to validate your JSON-LD, I have used the Google's structured tool JSON-LD validator, available at https://validator.schema.org/. I think you may find it useful in the future.
Your snippet passed the tests with zero errors or warnings. Just be careful with "logo.svg", because it is a relative path.I would add the full URL to your logo. The same applies for img.jpg

Can you use separate files for json subschemas?

I am new to using JSON schemas and I am pretty confused about subschemas. I have done many searches and read https://json-schema.org/understanding-json-schema/structuring.html but I feel like I am not getting some basic concepts.
I'd like to break up a schema into several files. For instance, I have a metric schema that I would like nested in a category schema. Can a subschema be a separate file that is referenced or is it a block of code in the same file as the base schema? If they are separate files, how do you reference the other file? I have tried using a lot of various values for $ref with the $id of the nested file but it doesn't seem to work.
I don't think I really understand the $id and $schema fields. I have read the docs on them but leave still feeling confused. Does the $id need to be a valid URI? The docs seem to say that they don't. And I just copied the $schema value from the jsonschema site examples.
Any help would be appreciated about what I am doing wrong.
(added the following after Ether's reply)
The error messages I get are:
KeyError: 'http://mtm/metric'
and variations on
jsonschema.exceptions.RefResolutionError: HTTPConnectionPool(host='mtm', port=80): Max retries exceeded with url: /metric (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe9204a31c0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))
Here is the category schema in category_schema.json:
{
"$id": "http://mtm/category",
"$schema":"https://json-schema.org/draft/2020-12/schema",
"title":"Category Schema",
"type":"object",
"required":["category_name", "metrics"],
"properties": {
"category_name":{
"description": "The name of the category.",
"type":"string"
},
"metrics":{
"description": "The list of metrics for this category.",
"type":"array",
"items": {
"$ref": "/metric"
}
}
}
}
And here is the metric schema in metric_schema.json:
{
"$id": "http://mtm/metric",
"$schema":"https://json-schema.org/draft/2020-12/schema",
"title":"Metric Schema",
"description":"Schema of metric data.",
"type":"object",
"required": ["metric_name"],
"properties": {
"metric_name":{
"description": "The name of the metric in standard English. e.g. Live Views (Millions)",
"type":"string"
},
"metric_format": {
"description": "The format of the metric value. Can be one of: whole, decimal, percent, or text",
"type": "string",
"enum": ["integer", "decimal", "percent", "text"]
}
}
}
Yes, you can reference schemas in other documents, but the URIs need to be correct, and you need to add the files manually to the evaluator if they are not network- or filesystem-available.
In your first schema, you declare its uri is "http://mtm/category". But then you say "$ref": "/mtm/metric" -- since that's not absolute, the $id URI will be used as a base to resolve it. The full URI resolves to "http://mtm/mtm/metric", which is not the same as the identifier used in the second schema, so the document won't be found. This should be indicated in the error message (which you didn't provide).

How to add a data value to a cypress test

I am having issues adding a data value to a cypress test. I want to test if the data-values exist
The data-value is below:
data-value="{ "id": "1", "name": "GBR"}"
when I add the test
cy.get('[data-value="1"]').should('exist')
It always comes back with a syntax error, I've tried it a few different ways but no luck.
What is the correct way?
One way is you can directly verify the entire value { "id": "1", "name": "GBR"}
cy.get('selector').should('have.attr', 'data-value', `{ "id": "1", "name": "GBR"}`)

Validating correctness of $ref in json schema

The requirement is to validate given json schema that there are no dangling $ref pointing to the definitions within the file.
{
"$schema": "http://json-schema.org/draft-6/schema#",
"definitions": {
"date": {
"type": "string",
"pattern": "^(0?[1-9]|[12][0-9]|3[01])\\-(0?[1-9]|1[012])\\-\\d{4}$"
},
},
"properties": {
"my_date": {"$ref": "#/definitions/dat"}
}
}
Here, there is a typo in the reference (dat instead of date). I want to catch such instances rather than having a run time failure.
Library being used: https://github.com/java-json-tools/json-schema-validator
You could validate that the use of $ref resolves by digesting the JSON, recursivly extracting the value of $ref, splitting on slash, and checking the path exists.
This COULD get more complicated as you might have external references which target URLs.
I can't give you any code as I don't know JAVA. It doesn't seem like what you want is specifically available using that library.

How to use 'Ignore or Validate' in JSON file (karate framework)?

I have to verify that each response is in correct format. I've put in my feature:
And match each response contains { id: '#string', name: '#string', phone: '#number' etc..}
But I would like to put this in a JSON file, beacuse I need it several times in different features.
When I use 'Ignore or Validate' tags in JSON file, it doesn't work. Is it possible to do this?
Yes why not. First place the following in a file called item-schema.json.
{ "id": "#string", "name": "#string", "phone": "#number" }
Now all you need to do is:
And match each response contains read('item-schema.json')
Please go through the documentation of Reading Files carefully.