How to get the glob list editor in vscode extension settings - vscode-extensions

The settings editor can do this
which produces similar to this in the settings file
"files.exclude": {
"**/.classpath": true,
"**/.factorypath": true,
"**/.project": true,
"**/.settings": true,
"**/.zorg": true
},
In package.json this contribution
"print.folder.exclude": {
"default": [],
"type": "array",
"description": "%print.folder.exclude%"
},
just gets a link to edit the file as json.
Declaring it as an object doesn't help. How do I get the glob list editor?

I raised this as an issue on the vscode GitHub repository.
The maintainers told me how to do it. They updated the documentation which should appear in the next release.
"print.folder.exclude": {
"default": [
"bin",
"obj",
"**/*.bin",
"**/*.exe",
"**/*.dll",
"**/*.hex",
"**/*.pdb",
"**/*.pdf"
],
"type": "array",
"items": {
"type": "string"
},
"description": "%print.folder.exclude%"
},
This produces an array of strings rather than an object, but you get the same editor.

Related

jsonschema dependentSchema not validating

I am trying to learn json schema, but something isn't working out for me.
I'm trying to run the example from http://json-schema.org/understanding-json-schema/reference/conditionals.html#id4 for dependentSchemas, but it just doesn't validate.
I'm using this schema:
check_schema = {"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"credit_card": { "type": "number" }
},
"required": ["name"],
"dependentSchemas": {
"credit_card": {
"properties": {
"billing_address": { "type": "string" }
},
"required": ["billing_address"]
}
}
}
and this json, that should raise an error since it is missing the key billing_address:
check_dict={
"name": "John Doe",
"credit_card": 5555555555555555
}
but when I use jsonschema.validate(dic_check, schema_check) (with python, jsonschema package version 4.2.1), the validation passes with no issues.
What am I doing wrong here?
If you are using an implementation that doesn't support at least draft2019-09 of the specification, dependentSchemas won't be recognized as a keyword. In earlier versions (draft7 and before), that keyword was known as dependencies, with the same syntax (actually, dependencies was split into two, dependentSchemas and dependentRequired).
The details are described on the page you linked, https://json-schema.org/understanding-json-schema/reference/conditionals.html#dependentschemas.
If you still believe that what you have should work, I suggest you open a bug report on the implementation's issue queue.

How to get jsonschema to use a boolean of false OR attributes

I'm creating an Avatar in json schema, and in my app, if we want to hide the avatar, we pass it in as false, otherwise, an avatar has an image and a name.
In json schema, I specify avatar as
const avatarSchema = {
"type": ["object", "boolean"],
"required": [],
"additionalProperties": false,
"properties":{
"name": {
"type": "string"
},
"image": {
"type": "string",
"format": "url"
}
}
};
This doesn't work because if Avatar = true, 'image' does not exist on type 'Avatar'.
Property 'image' does not exist on type 'true'.
I don't want Avatar to every be true, it's either false or {image, name}, how can I tell json schema to operate this way?
Your explanation of why your schema doesn't work is not correct. The required, additionalProperties, and properties keywords only apply if the data being validated is an object. So, the schema should work as you want except that it can have value "true".
If the validator you are using is giving an error message like the one in your question, it is not validating correctly. You should file a bug report for that validator.
Anyway, the solution to your problem requires the anyOf keyword.
{
"anyOf": [
{ "enum": [false] },
{
"type": "object",
"required": ["name", "image"],
"additionalProperties": false,
"properties": {
"name": { "type": "string" },
"image": {
"type": "string",
"format": "url"
}
}
}
]
}

How to use definitions from external files in JSON Schema?

I'm trying to import the definitions from another json schema using $ref but getting the following error:
can't resolve reference ../base/definitions.schema.json#/definitions/datetime from id #
{
"$schema": "http://json-schema.org/draft-06/schema#",
"definitions": {
"datetime": {
"type": "string"
},
"name": {
"type": "string"
},
}
}
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"active": {"type": "boolean"},
"created_at": { "$ref": "../base/definitions.schema.json#/definitions/datetime" },
"name": { "$ref": "../base/base/definitions.schema.json#/definitions/name" },
"updated_at": { "$ref": "../base/definitions.schema.json#/definitions/datetime" }
},
"required": ["name"],
"type": "object"
}
Directory structure:
api
-- base
-- definitions.schema.json
-- country
-- country.schema.json
I have tried several combinations by using an absolute path, a file url and several other combinations of the path. Not sure what's going on.
Schema validator: ajv#5.1.1
You need to add schemas using "addSchema" method. $ref is resolved relative to "id" attribute ("$id" in draft-06), ajv doesn't (and can't) use file paths.
EDIT: added $ref section to docs.

In VS Code, how can I get JSON intellisense based on the files in a given directory?

My application includes JSON Files.
One JSON property common to all JSON files in my app is "type".
The value of the type property must be the file name of any handlebars (.hbs) file in my /templates/ directory.
My goal is to get intellisense as I type the value for a given "type" property in a JSON file. The intellisense should include a list of all handlebars file names in the /templates/ directory. Either an automatic solution, or one where I provide a list of values would be great.
Is there a way in the VS Code config or with TypeScript to make that happen?
You can add a JSON schema to you workspace settings.json in .vscode directory.
This is an example that adds intellisense to package.json for ghooks settings:
"json.schemas": [
{
"fileMatch": [
"/package.json"
],
"schema": {
"type": "object",
"properties": {
"config": {
"properties": {
"ghooks": {
"type": "object",
"description": "Git hook configurations",
"properties": {
"applypatch-msg": {
"type": "string"
},
"pre-applypatch": {
"type": "string"
},
"post-applypatch": {
"type": "string"
},
"pre-commit": {
"type": "string"
},
"prepare-commit-msg": {
"type": "string"
},
"commit-msg": {
"type": "string"
},
"post-commit": {
"type": "string"
},
"pre-rebase": {
"type": "string"
},
"post-checkout": {
"type": "string"
},
"post-merge": {
"type": "string"
},
"pre-push": {
"type": "string"
},
"pre-receive": {
"type": "string"
},
"update": {
"type": "string"
},
"post-receive": {
"type": "string"
},
"post-update": {
"type": "string"
},
"pre-auto-gc": {
"type": "string"
},
"post-rewrite": {
"type": "string"
}
}
}
}
}
}
}
}
],
To get intellisense for specific property values you can check out this documentation: https://github.com/json-schema/json-schema/wiki/anyOf,-allOf,-oneOf,-not#oneof
For your usecase it could also be useful to dynamically generate the JSON schema that includes all possible template names and include the schema in your JSON file via $schema property or via configuration:
"json.schemas": [
{
"fileMatch": [
"/json-files/*.json"
],
"url": "./templates.schema.json"
}
],
I'm not aware of any vscode feature to dynamically generate such a schema. You might want to use an approach as I did in this extension: https://github.com/skolmer/vscode-i18n-tag-schema - an extension that uses a node module to dynamically generate a schema file.

Swagger integration into Dropwizard

I am fairly new to drop wizard (dropwizard.io) & just completed their tutorial. I would like to integrate Swagger (swagger.io) into this sample app.
I found:
github.com/federecio/dropwizard-swagger-sample-app
github.com/federecio/dropwizard-swagger
The generated JSON seems to be quite similar, however I cannot expand the REST resources to see their respective operations.
The only difference I noted was that the example code of the swagger integration uses SERVER whereas the official drop wizard example is using APPLICATION
Here an Image (i.stack.imgur.com/QzhPa.png)
Please could you tell me what is wrong in my approach.
Thank you very much. Here is the code to my approach: https://github.com/geoHeil/dropwizardSwaggerIntegrationNotWorking
Edit:
for api - docs {
"apiVersion": "0.0",
"swaggerVersion": "1.2",
"apis": [{
"path": "/sample"
}, {
"path": "/hello-world",
"description": "Operations about greetings"
}]
}
for sample {
"apiVersion": "0.0",
"swaggerVersion": "1.2",
"basePath": "http://geoHeil.local:8080",
"resourcePath": "/sample",
"apis": [{
"path": "/sample",
"operations": [{
"method": "GET",
"summary": "Sample endpoint",
"notes": "",
"type": "void",
"nickname": "get",
"authorizations": {},
"parameters": []
}]
}, {
"path": "/sample/hello-with-path-param/{name}",
"operations": [{
"method": "GET",
"summary": "Sample endpoint with path param",
"notes": "",
"type": "void",
"nickname": "getWithPathParam",
"authorizations": {},
"parameters": [{
"name": "name",
"required": true,
"type": "string",
"paramType": "path"
}]
}]
}, {
"path": "/sample/hello-with-query-param",
"operations": [{
"method": "GET",
"summary": "Sample endpoint with query param",
"notes": "",
"type": "void",
"nickname": "getWithQueryParam",
"authorizations": {},
"parameters": [{
"name": "name",
"required": false,
"type": "string",
"paramType": "query"
}]
}]
}]
}
for hello - world {
"apiVersion": "0.0",
"swaggerVersion": "1.2",
"basePath": "http://geoHeil.local:8080",
"resourcePath": "/hello-world",
"apis": [{
"path": "/hello-world",
"operations": [{
"method": "GET",
"summary": "Greetings endpoint",
"notes": "",
"type": "void",
"nickname": "sayHello",
"authorizations": {},
"parameters": [{
"name": "name",
"required": false,
"items": {
"type": "string"
},
"paramType": "query"
}]
}]
}]
}
There is now a Swagger spec 2.0 example available:
https://github.com/swagger-api/swagger-samples/tree/master/java/java-dropwizard
This uses the latest swagger core libraries with minimal dependencies. Note, the version of Jackson needed to be updated from dropwizard's 2.3.2 to swagger's 2.4.2.
I don't see anything obvious in the generated JSON that would cause the operations to not expand.
However, the swagger-ui bundled with the dropwizard-swagger package is a bit old. I would try using the newer version of swagger-ui.
I'm not sure if it would conflict with the bundled swagger-ui or not, but basically you need to clone https://github.com/swagger-api/swagger-ui and copy the /dist directory to your static content.
Another option is to run the swagger-ui locally (just for testing purposes) but opening the /dist/index.html and pointing it at your /api-docs directory. However, in order for that to work properly, you'd need to enable CORS - https://github.com/swagger-api/swagger-ui#cors-support.
EDIT:
I didn't notice you edited the question with the JSON, and it made it easier to read.
There's a problem with the GET /hello-world operation. It looks like it's supposed to accept an array of strings as a query parameter, but the parameter is missing a "type": "array" in its definition. I can't say what may be causing it without seeing the method's declaration and its annotations, but that's what you should be looking at.