---
title: "validate(): function in Diagnostics, TypeShade API reference"
description: "validate, a function in Diagnostics. Check an authored module against the structural rules every emit depends on…"
url: https://typeshade.dev/api/validate/
language: en
---

# `validate()`

Function in Diagnostics

Check an authored module against the structural rules every emit depends on, and throw a ValidationError when any of them fails.

`import { validate } from 'typeshade'`

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
function validate(m: ModuleDecl): void
```

## Parameters

**`m` `ModuleDecl`**

the authored module, as built, before any pass has rewritten its nodes.

## Return value

`void`

## Exceptions

**`@link ValidationError`**

carrying every error-severity diagnostic, with code [`SD0020`](https://typeshade.dev/reference/errors/sd0020/).

**`ValidationError`**

Check an authored module against the structural rules every emit depends on, and throw a ValidationError when any of them fails. emitModule, emitGlslModule and compileModule each run this check first, before they rewrite the module for their target, so a structurally invalid module is reported at the line that authored it and never reaches the driver as invalid WGSL or GLSL.

## Description

[`emitModule`](https://typeshade.dev/api/emitmodule/), [`emitGlslModule`](https://typeshade.dev/api/emitglslmodule/) and [`compileModule`](https://typeshade.dev/api/compilemodule/) each run this check first, before they rewrite the module for their target, so a structurally invalid module is reported at the line that authored it and never reaches the driver as invalid WGSL or GLSL. Call it directly when you want that answer without emitting.

One error reports every failure. `validate` collects every error-severity diagnostic, and the throw carries them all on `err.diagnostics`; the message renders the same list as text. Each diagnostic names its `code` ([`SD0107`](https://typeshade.dev/reference/errors/sd0107/) and its siblings), the `ruleId` that raised it, the `fn` it was found in, and, with `setSourceTracing` on, the `file:line:col` that authored the node.

It runs the core rules: duplicate struct or function names, binding collisions, a function path that ends without returning a value, mixed scalar types in one expression, a call whose arity or argument types disagree with the function it names, a fragment-only builtin read outside a fragment entry, a portable kernel that cannot emit on both backends, and a local that shadows another in the same function. These hold for every module, including modules assembled with [`composeModule`](https://typeshade.dev/api/composemodule/) and compute kernels that return early on purpose. Style rules are lint-only; run `lintModule` or `diagnose` for those.

## Examples

### Example

```ts
import { validate, ValidationError } from 'typeshade'

try {
  validate(MODULE)
} catch (e) {
  if (e instanceof ValidationError) for (const d of e.diagnostics) report(d.code, d.message)
  else throw e
}
```

## In the guide

- [Diagnostics](https://typeshade.dev/guide/internals/diagnostics/)

## See also

- [`ValidationError`](https://typeshade.dev/api/validationerror/)
- [`emitModule`](https://typeshade.dev/api/emitmodule/)
- [`emitGlslModule`](https://typeshade.dev/api/emitglslmodule/)
- [`compileModule`](https://typeshade.dev/api/compilemodule/)
- [`composeModule`](https://typeshade.dev/api/composemodule/)

## Source

[`src/core/passes/validate.ts`, line 119](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/validate.ts#L119), at commit af54ddad5
