You are reading the development docs (2026.12.1-dev.1): features here may not be released yet.
Skip to content

Validation Service ​

Config schema validation for all LCARdS card types: window.lcards.core.validationService


Overview ​

Runtime config validation is split across two singletons:

  • CoreConfigManager (window.lcards.core.configManager) owns the per-card-type schema registry (registerCardSchema() / getCardSchema()) and validates automatically as the last step of processConfig(). Its merge pipeline has its own page: Config Manager.
  • CoreValidationService (window.lcards.core.validationService, src/core/validation-service/index.js) does the structural validation, validate(data, schema, context). It also holds optional token-reference and data-source-reference validators, and a small internal registry of generic schemas (card-config, entity-reference, action-config, position, size).

CoreValidationService is a plain class (it does not extend BaseService). Core creates it in _performInitialization() with { validateEntities: true, cacheResults: true, debug: false }, calls initialize(hass), and passes hass updates to updateHass() from LCARdSCore._updateHass() (src/core/lcards-core.js).

This is the runtime validator that runs in the browser. The build and the user CLI use a different checker: scripts/lib/config-validator.js (ajv over the published schemas, plus unknown keys and registry names). Both read the same card schemas from src/cards/schemas/.


Key files ​

FileRole
src/core/validation-service/index.jsCoreValidationService (validate()), plus the internal CoreSchemaRegistry (5 generic schemas) and CoreErrorFormatter (applied inside validate())
src/core/validation-service/TokenValidator.jsChecks theme-token references against a ThemeManager; only through validationService.validateTokens()
src/core/validation-service/DataSourceValidator.jsChecks source / data_source / sources on data-source-capable config types; only through validationService.validateDataSources()
src/core/validation-service/ErrorFormatter.jsTurns a validation result into a multi-line string, for validationService.formatErrors()
src/core/config-manager/index.jsCoreConfigManager: schema registry (_cardSchemas), merge pipeline, _validateConfig() which calls validationService.validate()
src/cards/schemas/*.jsPer-card schema definitions (button-schema.js, msd-schema.js, ...); shared parts in common-schemas.js
src/cards/schemas/card-plumbing.jsCARD_PLUMBING_KEYS: Home Assistant layout keys and card-mod / UIX keys accepted at any card root

Schema registration ​

Each card class has a static registerSchema(). src/lcards-main.js calls them once, after core initialisation and the custom element definitions:

javascript
if (window.lcards?.core?.configManager) {
    if (LCARdSButton.registerSchema) LCARdSButton.registerSchema();
    if (LCARdSElbow.registerSchema) LCARdSElbow.registerSchema();
    if (LCARdSChart.registerSchema) LCARdSChart.registerSchema();
    if (LCARdSSlider.registerSchema) LCARdSSlider.registerSchema();
    if (LCARdSDataGrid.registerSchema) LCARdSDataGrid.registerSchema();
    if (LCARdSMSDCard.registerSchema) LCARdSMSDCard.registerSchema();
    if (LCARdSAlertOverlay.registerSchema) LCARdSAlertOverlay.registerSchema();
    if (LCARdSSelectMenu.registerSchema) LCARdSSelectMenu.registerSchema();
    if (LCARdSLayoutCard.registerSchema) LCARdSLayoutCard.registerSchema();
    if (LCARdSAstrometricsCard.registerSchema) LCARdSAstrometricsCard.registerSchema();
}

The schemas are registered under the card's CARD_TYPE (the key processConfig() is called with), not the custom element name:

CardRegistered as
Buttonbutton
Elbowelbow
Chartchart
Sliderslider
Data griddata-grid
MSDmsd
Alert overlayalert-overlay
Select menuselect-menu
Layout cardlayout-card
Astrometricsastrometrics: its registerSchema() only registers the defaults ({ height: 400 }); the schema is a lazy chunk registered on first use by ensureAstrometricsSchema() (src/cards/lcards-astrometrics-card.js), so it stays out of the main bundle

Inside registerSchema() the card builds its schema with the runtime option lists (presets, components, positions) and registers it:

javascript
static registerSchema() {
    const configManager = window.lcards?.core?.configManager;
    if (!configManager) {
        lcardsLog.error('[MyCard] CoreConfigManager not available for schema registration');
        return;
    }
    const schema = getMyCardSchema({ /* runtime options, e.g. available presets */ });
    configManager.registerCardSchema('my-card', schema, { version: __LCARDS_VERSION__ });
    // Behavioural defaults, if the card has any (see the Config Manager page)
    configManager.registerCardDefaults('my-card', { orientation: 'horizontal' });
}

To add a new card type to validation:

  1. Add a schema file at src/cards/schemas/my-card-schema.js, every property with a description.
  2. Add static CARD_TYPE = 'my-card' and static registerSchema() to the card class.
  3. Call MyCard.registerSchema() in src/lcards-main.js.
  4. Add the schema to scripts/lib/card-schemas.js so the doc validator, the description check and the published schema know it.

Once registered, every processConfig(userConfig, 'my-card', context) validates the merged config against that schema; with no schema registered, validation is skipped and the result is valid: true.


What validate() checks ​

validate(data, schema, context = {}):

  1. counts the call, builds a cache key JSON.stringify(data) + schema + JSON.stringify(context) and returns a cached result if config.cacheResults (a hit moves to the most recently used end). A schema name is used as is; an inline schema object (a card's own) is keyed by identity, a short id from a WeakMap (_schemaId()), since its JSON was megabytes per key;
  2. looks up a string schema in CoreSchemaRegistry (unknown name: schema_not_found error);
  3. walks the schema (_validateAgainstSchema());
  4. if config.validateEntities and hass is present, checks entity references (_validateEntities());
  5. sets valid = errors.length === 0 (warnings never make a result invalid; there is no strict mode);
  6. runs every error and warning through CoreErrorFormatter.formatError(), caches the result, returns it. The result cache keeps at most CoreValidationService.MAX_CACHED (500) entries and drops the least recently used first: every new card config is a new key, and the unbounded cache grew a long-lived tab by hundreds of MB (src/core/validation-service/index.js; tests/core/validation-cache.test.js).

An exception inside gives a validation_error error.

The walker is hand-written, not ajv. It supports:

Schema featureBehaviour
type (one or a list)A list dispatches on the value's runtime type
oneOf / anyOfPasses when any option matches (a multi-match oneOf is tolerated); the matching branch's warnings surface, and a deprecated branch warns
propertiesEach present property is validated; discriminatedBy picks the sub-schema from a sibling field
required: true on a propertyMissing: required_field error
required: [...] (array form)Missing: missing_recommended_field warning (never enforced as an error, so older saved configs are not rejected)
additionalProperties: falseUnknown key: invalid_property error
additionalProperties: 'warn'Unknown key: invalid_property warning
deprecated (with x-replacement)deprecated_property warning
enum, pattern, minimum, maximum, minLength, maxLengthinvalid_enum, invalid_format, out_of_range errors
items, minItems, maxItemsArray checks
Root CARD_PLUMBING_KEYSAlways accepted at the root

Entity checks (_findEntityReferences()): only values under the keys entity, entity_id and entities that look like domain.object are checked, and nothing under a .card., .card_config. or .cardConfig. path (nested cards validate their own). A missing entity is a missing_entity warning, never an error. The check is skipped while hass.states is empty. Results are cached per entity id in entityCache, which updateHass() clears; the result cache (validationCache) is not cleared on hass updates.


Public API ​

CoreConfigManager (schema registry) ​

MethodReturnsDescription
registerCardSchema(cardType, schema, options?)voidRegister a schema; options.version defaults to '1.0'
getCardSchema(cardType)Object | nullThe registered schema
processConfig(userConfig, cardType, context?)Promise<{ valid, mergedConfig, errors, warnings, provenance }>Merge, resolve tokens, validate. See Config Manager

registerCardDefaults(), updateContext() and the merge layers are on the Config Manager page.

CoreValidationService ​

MethodReturnsDescription
initialize(hass?)Promise<void>Stores hass, sets initialized
validate(data, schema, context?){ valid, errors, warnings, data, schema }Validate against a schema object or a registry name ('card-config', 'entity-reference', 'action-config', 'position', 'size'); schema in the result is the name or 'inline'
validateTokens(config, context?){ errors, warnings }Runs TokenValidator; needs setThemeManager() first, else returns empty lists. Not called by validate()
validateDataSources(config, context?){ errors, warnings }Runs DataSourceValidator; needs setDataSourceManager() first. Not called by validate()
setThemeManager(themeManager)voidCreates tokenValidator
setDataSourceManager(dataSourceManager)voidCreates dataSourceValidator
formatErrors(validationResult)stringMulti-line summary via ErrorFormatter
setCaching(enabled)voidSets config.cacheResults; false also clears the caches
clearCache()voidClears the result cache and the entity cache
updateHass(hass)voidStores hass, clears the entity cache
getStats()Object{ validationsPerformed, errorsFound, warningsFound, cacheHits, entityChecks }
getDebugInfo()ObjectSee Console access
destroy()voidClears caches and hass
javascript
// The common path: card types validate through configManager
const configManager = window.lcards.core.configManager;
const result = await configManager.processConfig(this._rawConfig, 'button', { hass: this.hass });
if (!result.valid) {
    lcardsLog.warn('[MyCard] Config validation failed:', result.errors);
}

// Direct structural validation against any schema object
const vs = window.lcards.core.validationService;
const direct = vs.validate({ entity: 'light.bedroom' }, myJsonSchema);

Who calls it ​

CallerWhat it does
CoreConfigManager._validateConfig()validate(mergedConfig, schema, { context: cardType }) for every processConfig() (cards, MSD)
LCARdSBaseEditor._validateConfigWithSingleton() (src/editor/base/LCARdSBaseEditor.js)validate(config, this._getSchema(), { cardType, source: 'editor' }); used by _validateConfig() and on YAML edits, where only a valid result is committed and the errors feed the YAML tab's markers (_validationErrors)
MSD pipeline (initMsdPipeline(), src/msd/pipeline/PipelineCore.js)Does not call validate() itself: MSD configs are validated by processConfig() through ConfigProcessor.processAndValidateConfig(), and errors there disable the pipeline. It hands the shared service to the coordinator (below)

The MSD pipeline does not write the service's config. It calls setThemeManager() / setDataSourceManager() with the coordinator's managers (the core singletons), stores the service as coordinator.validationService and publishes it as window.lcards.validationService. config is shared by every card and is not set for one.


Console access ​

javascript
window.lcards.debug.singleton('validationService')
// {
//   initialized: true,
//   hasHASS: true,
//   config: {
//     validateEntities: true, cacheResults: true, debug: false,
//     validateTokens: true, validateDataSources: true, stopOnError: false
//   },
//   stats: { validationsPerformed, errorsFound, warningsFound, cacheHits, entityChecks },
//   cacheSize: 24,
//   entityCacheSize: 12,
//   availableSchemas: ['card-config', 'entity-reference', 'action-config', 'position', 'size']
// }
javascript
const vs = window.lcards.core.validationService;

vs.validate(config, schema, context);     // structural validation
vs.getStats();
vs.clearCache();
vs.setCaching(false);                     // re-validate on every call while debugging
vs.formatErrors(result);

const cm = window.lcards.core.configManager;
cm.getCardSchema('button');               // registered schema for a CARD_TYPE
cm.getDebugInfo().registeredCards;        // { schemas: [...], defaults: [...] }

availableSchemas lists the generic schemas built into CoreValidationService, not the card schemas; those are in configManager (cm.getCardSchema('button')). config.validateTokens and config.validateDataSources are flags only: nothing reads them.


Error output ​

validate() (and processConfig(), which passes its lists through) returns:

javascript
{
  valid: boolean,
  errors:   Array<{ type, field, message, formattedMessage, suggestion, severity, context }>,
  warnings: Array<{ type, field, message, formattedMessage, suggestion, severity, context }>
}

Entries are objects. formattedMessage and suggestion come from CoreErrorFormatter templates where one exists for the type, else 'Validation error: {message}' and 'Check your configuration':

typeListformattedMessage template
required_fielderrorsRequired field "{field}" is missing
invalid_typeerrorsField "{field}" must be {expected}, got {actual}
invalid_enumerrorsField "{field}" must be one of: {validValues}
invalid_formaterrorsField "{field}" has invalid format
out_of_rangeerrorsField "{field}" value {value} is out of valid range
invalid_propertyerrors or warnings (additionalProperties: false / 'warn')generic (Validation error: Unexpected property "...")
missing_recommended_fieldwarningsgeneric
deprecated_propertywarningsgeneric
unrecognized_discriminatorwarningsgeneric
missing_entitywarningsEntity "{entity}" not found in Home Assistant
schema_not_found, validation_errorerrorsgeneric

TokenValidator ​

TokenValidator (constructor(themeManager), validate(config, context)) walks a config for theme-token-shaped strings and checks that each resolves against the active theme. It exists on validationService.tokenValidator after setThemeManager() (today only the MSD pipeline calls that). Nothing calls validateTokens() automatically.


Error display ​

  1. Browser console: LCARdSCard._processConfigAsync() logs result.errors with lcardsLog.error and result.warnings with lcardsLog.warn, and applies the merged config either way.
  2. Card editor: the YAML tab shows the errors from _validateConfigWithSingleton() inline and does not commit an invalid YAML edit.

See also ​

Released under the MIT License. | About