Validation Service
Config schema validation for all LCARdS card types —
window.lcards.core.validationService
Overview
The Validation Service provides runtime config validation using a schema registry. When a card receives a config object, validation catches structural errors and unknown fields early, before rendering starts.
Key Files
| File | Role |
|---|---|
core/validation-service/index.js | Service entry point |
core/validation-service/SchemaRegistry.js | Stores schemas keyed by card type; register/lookup |
core/validation-service/DataSourceValidator.js | Validates data_sources block |
core/validation-service/OverlayValidator.js | Validates MSD overlay definitions |
core/validation-service/TokenValidator.js | Detects invalid token syntax in string fields |
core/validation-service/ValueValidator.js | Type + range checking for scalar fields |
core/validation-service/ErrorFormatter.js | Formats error objects into human-readable messages |
core/validation-service/schemas/ | Per-card JSON schema definitions |
Schema Registration
Each card class exposes a static registerSchema() method. These are called in src/lcards.js after all core services initialise:
// lcards.js (called once at startup, after core is ready)
if (LCARdSButton.registerSchema) LCARdSButton.registerSchema();
if (LCARdSElbow.registerSchema) LCARdSElbow.registerSchema();
// ... one call per card typeInside registerSchema(), the card calls configManager.registerCardSchema():
// Inside your card class
static registerSchema() {
const configManager = window.lcards?.core?.configManager;
if (!configManager) return;
const schema = getMyCardSchema({ /* runtime options, e.g. available presets */ });
configManager.registerCardSchema('my-card', schema, { version: __LCARDS_VERSION__ });
}To add a new card type to validation:
- Add a schema file at
src/cards/schemas/my-card-schema.js - Add
static registerSchema()to your card class callingconfigManager.registerCardSchema('my-card', schema) - Call
MyCard.registerSchema()insrc/lcards.jsafter core initialisation
Public API
| Method | Returns | Description |
|---|---|---|
validateConfig(cardType, config) | { valid, errors, warnings } | Validate a config object against the card's registered schema |
getSchema(cardType) | Object|null | Retrieve the raw JSON schema for a card type |
getRegisteredTypes() | string[] | All card types with registered schemas |
import { validateConfig } from '../core/validation-service/index.js';
// Returns { valid: boolean, errors: string[], warnings: string[] }
const result = validateConfig('lcards-button', this.config);
if (!result.valid) {
lcardsLog.warn('[MyCard] Config validation failed:', result.errors);
}Console Access
window.lcards.debug.singleton('validationService')
// → { type: 'ValidationService', initialized: true, registeredSchemas: 8, cacheSize: 14 }const vs = window.lcards.core.validationService
vs.getRegisteredTypes() // all card types with schemas
vs.getSchema('lcards-button') // raw schema object
vs.validateConfig('lcards-slider', {}) // run validation directlyTokenValidator
TokenValidator validates {theme:token.path} references used in card config fields. Given a theme manager, it checks that the token path exists in the active theme, resolves to a valid value, and does not form a circular reference. It is applied automatically during config validation — you do not need to call it directly.
Error Output Format
validateConfig() returns:
{
valid: boolean,
errors: string[], // blocking issues — card should not render
warnings: string[] // non-fatal suggestions
}Example error messages (generated by ErrorFormatter):
Required field "entity" is missing
Field "style.color" must be string, got number
Field "preset" must be one of: lozenge, pill, bullet, rectangle
Field "style.color" value 999 is out of valid range
Entity ID "light.bedroom_lamp" is not valid (use format: domain.entity_id)Error Display
Validation errors surface in two places:
- Browser console — structured log messages with field paths
- Card editor — the YAML tab shows inline error markers via CodeMirror integration