---
title: Validate a query
summary: Check that a query parses and uses only available features before running it, from the SDKs or with an empty model set over HTTP, and read the line and column of what fails and the warnings, with suggestions, of a query that parses.
order: 40
languages: [javascript, java, php]
terms: [version, bql]
see-also: [how-to/query/run-a-bql-query, reference/bql, tutorials/bql, explanation/the-query-languages]
---

# Validate a query

For a query editor, a saved-query feature or a test suite: find out whether a query is acceptable without touching a model. Validation parses the query, applies the same augmentation the real run applies, and checks the features it uses against the instance (for example that the headroom feature is enabled); it does not check whether the models contain anything. A query that does not parse fails and says on which line and column, up to three problems at once; a query that parses comes back with the validator's warnings about it, such as an IFC class in the wrong case, each with what was probably meant.

## Live demo

```demo
name: dsl-errors
title: Validate a query and read the message
height: 360
needs: No model is loaded; the query is validated by the server. Open BIM.works and sign in once in this browser.
notes: |
  The buttons load sample queries, valid and broken; **Validate** (or Ctrl+Enter in the editor) sends the text to the server and shows the message that comes back.
```

## Before you start

- A login with the `query:run` scope.

## 1. Validate

```code-group {#validate} classic
```javascript
try {
	const {messages} = await client.bim.validateQuery("GET qid, attribute.Name WHERE type = IFCWALL");
	console.log(messages.length === 0 ? "valid" : messages);
} catch (e) {
	const error = e.error || e.response?.error || e;
	console.log(error.domain, error.type);   // BimWorksQueryError SYNTAX_ERROR
}
```
```java
boolean valid = client.bim.validate("GET qid, attribute.Name WHERE type = IFCWALL");
// The same check, with the validator's warnings about a query that parses
List<BimApi.QueryValidationMessage> messages = client.bim.validateQuery("GET qid, attribute.Name WHERE type = IFCWALL");
```
```

```sdk-example
operation: Query
body: |
  {
    "models": [],
    "query": "GET qid, attribute.Name WHERE type = IFCWALL"
  }
```

`validateQuery` on [[js:@bimworks/jssdk/Bim|Bim]] rejects with the error for a query that does not parse and otherwise resolves with `{messages}`, the validator's warnings (step 2), empty for a clean query. `BimApi.validate` on [[java:nl.bimbase.bimworks.client.apis.BimApi|BimApi]] returns `false` for a query the server rejects and throws `BimWorksException` for any other failure (no rights, no connection); `BimApi.validateQuery` returns the messages and throws `BimWorksException` for a query that does not parse. All of them call the `ValidateQuery` task. The .NET SDK has no validation call; use the HTTP route.

Over HTTP, [[openapi:Query]] with an empty `models` array validates: a `200` with an empty result means the query parses and its features are available, a `400` with `SYNTAX_ERROR` means it does not parse. This route has no room for the validator's warnings: `WHERE type = IfcWall` gets the same empty `200` here and fails with [[error:UNKNOWN_TYPE]] once it runs on a model.

```try-it
operation: Query
method: POST
path: /openapi/ifc/Query
body: |
  {
    "models": [],
    "query": "GET qid, attribute.Name WHERE type = IFCWALL"
  }
```

## 2. Read the errors

The message is the same everywhere: in the API error, in the editor markers of the app, in the SDKs, and in the assistant that repairs actions in the chat. Show it to the user as it is; in JavaScript it is the `userSafeMessage` of the error:

```javascript {#validate-api}
try {
	await client.bim.validateQuery("GET attribute.Name\nWHER type = IFCWALL");
} catch (e) {
	const error = e.error || e.response?.error || e;
	console.log(error.userSafeMessage || error.message);   // line 2:1: ...
}
```

A query that parses can still be wrong. What the validator finds in it comes back in `messages`, each with a `severity` (`ERROR` or `WARNING`, as the editor marks it), the `code` of the check that found it, the `line` and `column`, and the same `message` the editor shows:

```javascript {#validate-messages}
const {messages} = await client.bim.validateQuery("GET attribute.Name\nWHERE type = IfcWall");
for (const message of messages) {
	console.log(message.severity, message.code, message.message);
	// WARNING unknownIfcClass line 2:14: IfcWall is not a known IFC class (IFC class names are uppercase); did you mean IFCWALL?
}
```

- **Positions.** A syntax error reads `line 3:14: mismatched input '…'`, and a query with several errors lists up to three of them, separated by `;`. A text that is not BQL at all fails at its first token: `line 1:1: no viable alternative at input 'THIS'`. Every parse error is reported; none produces a broken query that fails later.
- **Suggestions.** The validation knows the schema: an IFC class in the wrong case in `WHERE type = IfcWall` gets *is not a known IFC class (IFC class names are uppercase); did you mean IFCWALL?*, an attribute in the wrong case is told that attribute names are case-sensitive, with the right spelling. About thirty semantic checks — regular-expression validity, `BETWEEN` bounds, `FIRST` without `GROUP BY`, unit pitfalls — come back as the `messages` of the `ValidateQuery` task (the SDK calls above), as markers in the web editor, and to the assistant in the chat. The query still validates, so a client that only tests for success keeps working; the [[openapi:Query]] route with an empty model set does not return them. The other languages' checks, such as the closest existing name for a misspelt block or configuration key in a BRL report, show in their editors and in the chat.
- **Refusals.** A feature that a workspace visibility rule hides from you comes back the same way, as an `ERROR` message with the code [[error:FEATURE_NOT_VISIBLE]]: the query parses, but `Query2` refuses to run it.
- **Every language.** BQL, BVL, BUL, BCL, BRL, BSL, BCOL and BFL report their errors the same way ([[page:explanation/the-query-languages#errors-that-say-where]]). In the app, the action editors mark the line in the gutter and show the message, and the *Validate* button of a query does the same without running it. Running an invalid query with `Query2` reports the same message instead of running.

The codes:

- [[error:SYNTAX_ERROR]]: the text does not parse; the message names the position and what was expected.
- [[error:FEATURE_DISABLED]]: a feature the instance has switched off.
- [[error:FEATURE_NOT_VISIBLE]]: the code of the `ERROR` message for a feature a visibility rule hides; the query validates, running it is refused.
- [[error:UNKNOWN_TYPE]]: an IFC class that is not in the IFC schemas, raised when the query runs; validation warns about it beforehand with the `unknownIfcClass` message.
- [[error:MISSING_QUERY]]: a run (`Query2`, `QueryCount`) without a query.

The complete list is on [[page:reference/errors]] under `BimWorksQueryError` and `BimWorksErrorCode`.

## Check

`GET qid WHERE type = IFCWALL` validates with no messages; `GET qid WHERE type = IfcWall` validates with one `WARNING`, `unknownIfcClass`, that suggests `IFCWALL`; `GET qid WHERE type = IFCWALL AND` (a dangling `AND`) fails with [[error:SYNTAX_ERROR]]; running the valid query afterwards ([[page:how-to/query/run-a-bql-query]]) returns objects.
