|
| 1 | +# Windmill YAML Validator |
| 2 | + |
| 3 | +A TypeScript-based YAML validator for Windmill flow files. This package validates flow.yaml files against the OpenFlow JSON schema to ensure they conform to the Windmill flow specification. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The windmill-yaml-validator provides runtime validation for Windmill flow YAML files. It is currently used in the **Windmill VSCode extension** to show syntax errors and validation issues on `flow.yaml` files in real-time as developers edit them. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Schema-based validation**: Validates against the official OpenFlow JSON schema |
| 12 | +- **Detailed error reporting**: Returns comprehensive error information with specific paths to invalid fields |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install windmill-yaml-validator |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Basic Validation |
| 23 | + |
| 24 | +```typescript |
| 25 | +import { FlowValidator } from "windmill-yaml-validator"; |
| 26 | + |
| 27 | +const validator = new FlowValidator(); |
| 28 | + |
| 29 | +const yamlContent = ` |
| 30 | +summary: Test Flow |
| 31 | +value: |
| 32 | + modules: [] |
| 33 | +`; |
| 34 | + |
| 35 | +const result = validator.validateFlow(yamlContent); |
| 36 | + |
| 37 | +if (result.errors.length === 0) { |
| 38 | + console.log("Flow is valid!"); |
| 39 | +} else { |
| 40 | + console.log("Validation errors:", result.errors); |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +### Error Handling |
| 45 | + |
| 46 | +The validator returns detailed error information for invalid flows: |
| 47 | + |
| 48 | +```typescript |
| 49 | +const invalidYaml = ` |
| 50 | +summary: 123 # Should be a string |
| 51 | +value: |
| 52 | + modules: |
| 53 | + - id: step1 |
| 54 | + value: |
| 55 | + type: rawscript |
| 56 | + language: invalid_language # Invalid enum value |
| 57 | +`; |
| 58 | + |
| 59 | +const result = validator.validateFlow(invalidYaml); |
| 60 | + |
| 61 | +result.errors.forEach((error) => { |
| 62 | + console.log(`Error at ${error.instancePath}: ${error.message}`); |
| 63 | + // Example output: |
| 64 | + // Error at /summary: must be string |
| 65 | + // Error at /value/modules/0/value/language: must be equal to one of the allowed values |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +## API |
| 70 | + |
| 71 | +### `FlowValidator` |
| 72 | + |
| 73 | +Main validator class that validates Windmill flow YAML files. |
| 74 | + |
| 75 | +#### Constructor |
| 76 | + |
| 77 | +```typescript |
| 78 | +new FlowValidator(); |
| 79 | +``` |
| 80 | + |
| 81 | +Creates a new validator instance. The constructor initializes the AJV validator with the OpenFlow schema. |
| 82 | + |
| 83 | +#### Methods |
| 84 | + |
| 85 | +##### `validateFlow(doc: string)` |
| 86 | + |
| 87 | +Validates a flow document against the OpenFlow schema. |
| 88 | + |
| 89 | +**Parameters:** |
| 90 | + |
| 91 | +- `doc` (string): The YAML flow document as a string |
| 92 | + |
| 93 | +**Returns:** |
| 94 | + |
| 95 | +```typescript |
| 96 | +{ |
| 97 | + parsed: YamlParserResult<unknown>; // Parsed YAML with source pointers |
| 98 | + errors: ErrorObject[]; // Array of validation errors (empty if valid) |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +**Throws:** |
| 103 | + |
| 104 | +- Error if `doc` is not a string |
| 105 | + |
| 106 | +## Development |
| 107 | + |
| 108 | +### Building |
| 109 | + |
| 110 | +```bash |
| 111 | +npm run build |
| 112 | +``` |
| 113 | + |
| 114 | +The build process: |
| 115 | + |
| 116 | +1. Runs `gen_openflow_schema.sh` to generate the OpenFlow JSON schema from `openflow.openapi.yaml` |
| 117 | +2. Removes discriminator mappings (not supported by AJV) |
| 118 | +3. Compiles TypeScript to JavaScript |
| 119 | + |
| 120 | +### Testing |
| 121 | + |
| 122 | +```bash |
| 123 | +npm test |
| 124 | +``` |
| 125 | + |
| 126 | +Run tests in watch mode: |
| 127 | + |
| 128 | +```bash |
| 129 | +npm test:watch |
| 130 | +``` |
| 131 | + |
| 132 | +### Schema Generation |
| 133 | + |
| 134 | +The validator uses a JSON schema generated from the OpenAPI specification: |
| 135 | + |
| 136 | +```bash |
| 137 | +./gen_openflow_schema.sh |
| 138 | +``` |
| 139 | + |
| 140 | +This script: |
| 141 | + |
| 142 | +- Bundles `openflow.openapi.yaml` into a single JSON schema |
| 143 | +- Removes discriminator mappings for AJV compatibility |
| 144 | +- Removes the `ToolValue` discriminator entirely (see below) |
| 145 | +- Outputs to `src/gen/openflow.json` |
| 146 | + |
| 147 | +#### Why Remove Discriminators? |
| 148 | + |
| 149 | +The OpenFlow schema uses OpenAPI discriminators for efficient type resolution in `oneOf` schemas. However, AJV's discriminator support has limitations: |
| 150 | + |
| 151 | +1. **Discriminator Mappings**: Not fully supported by AJV, so they are removed from all schemas |
| 152 | +2. **ToolValue Discriminator**: Completely removed because `FlowModuleTool` uses `allOf` composition, which prevents AJV from finding the discriminator property (`tool_type`) at the expected location |
| 153 | + |
| 154 | +**Impact**: Without discriminators, AJV falls back to standard `oneOf` validation, which: |
| 155 | + |
| 156 | +- Tests each alternative until one matches |
| 157 | +- Is slightly slower but still performant for our use case |
| 158 | +- Provides the same validation correctness |
| 159 | +- Works correctly with complex schema compositions like `allOf` |
0 commit comments