kanopi/drupal-sdc-validator

CLI tool to validate Drupal Single Directory Component (.component.yml) files.

Installs: 70

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/kanopi/drupal-sdc-validator

v1.0.1 2025-10-22 03:53 UTC

This package is auto-updated.

Last update: 2025-10-22 03:54:45 UTC


README

A CLI tool to validate Drupal Single Directory Component (SDC) .component.yml files for structure and schema compliance.

This validator helps you ensure your SDC metadata files follow the Drupal core schema, and reports any missing fields, invalid structures, or schema violations.

๐Ÿš€ Features

  • โœ… Validates .component.yml files recursively in any path
  • โœ… Validates against Drupal core's metadata-full.schema.json
  • โœ… Checks for name collisions between props and slots
  • โœ… Validates non-string property types
  • โœ… Supports class/interface type validation (like Drupal core)
  • โœ… Optional --enforce-schemas flag for strict validation
  • โœ… JSON Schema validation via justinrainbow/json-schema
  • โœ… Works as a Composer-installed CLI tool (vendor/bin/validate-sdc)
  • โœ… Caches remote schema for 24 hours for faster re-runs
  • โœ… Error messages match Drupal core's ComponentValidator format

๐Ÿงฉ Installation

composer require --dev kanopi/drupal-sdc-validator

๐Ÿ“– Usage

Basic Usage

Validate components in one or more directories:

# Single directory
vendor/bin/validate-sdc web/themes/custom/your_theme/components

# Multiple directories
vendor/bin/validate-sdc web/themes/custom/theme1/components web/modules/custom/module1/components

Enforce Schema Validation

Use the --enforce-schemas flag to require schema definitions (similar to Drupal modules):

vendor/bin/validate-sdc web/modules/custom/your_module/components --enforce-schemas

This will fail validation if any component is missing a props schema definition.

Validation Modes

Default (Lenient Mode)

  • Components without props are valid (matches Drupal theme behavior)
  • Components with props are validated against the schema

Strict Mode (--enforce-schemas)

  • All components must have props defined (matches Drupal module behavior)
  • Use this for module components or when you want strict validation

Example Output

web/themes/custom/mytheme/components/button/button.component.yml has validation errors:
  โ€ข The component "button" declared [variant] both as a prop and as a slot. Make sure to use different names.
  โ€ข [props.properties.size.type] The property type must be a string.

============================================================
โœ— Validation failed!
  Total files checked: 15
  Files with errors: 1

๐Ÿ” Validation Rules

This validator implements the same validation logic as Drupal core's ComponentValidator:

Name Collision Detection

Checks that props and slots don't share the same names.

Non-String Type Validation

Ensures all property types are declared as strings (not integers, booleans, etc.).

Class/Interface Type Support

Validates custom class/interface types exist in the codebase (e.g., Drupal\Core\Url).

Schema Enforcement

With --enforce-schemas, requires all components to have prop schemas defined.

Empty Properties Handling

Properly handles empty properties: {} declarations.

๐Ÿงช Integration with Your Project

Add Composer Scripts

Once installed, add these scripts to your project's composer.json for easy access:

{
  "scripts": {
    "validate-sdc": [
      "@php vendor/bin/validate-sdc web/themes/custom"
    ],
    "validate-sdc-enforce": [
      "@php vendor/bin/validate-sdc web/themes/custom --enforce-schemas"
    ]
  }
}

Then run:

# Lenient mode (themes)
composer validate-sdc

# Strict mode (modules)
composer validate-sdc-enforce

Note: Using @php ensures exit codes are properly propagated, making validation failures fail CI/CD pipelines.

๐Ÿ“ License

MIT