AI Validation Generator
This module uses AI to suggest field validation rules based on plain-English descriptions, allowing site builders to review and enforce them. It works on any fieldable entity, supports various AI providers, and allows for manual editing, revocation, and restoration of rules without needing to write custom code.
Introduction
AI Validation Generator is a Drupal 11 module that suggests field validation rules for any fieldable content bundle using the
AI module's chat provider ecosystem. Instead of hand-writing regular expressions
and length/range checks for every field, site builders can pick a bundle and its fields, optionally add a plain-English hint,
and review an AI-generated set of validation rules before anything is enforced.
The module works with any fieldable entity type—not just nodes—including base fields such as a node's Title.
Every accepted rule set is enforced immediately on future entity saves, can be edited or authored entirely by hand without
ever calling the AI, and can be revoked (disabled without deleting) or restored at any time, individually or in bulk.
Project Summary
Suggest AI-powered field validation rules for any fieldable bundle. Review, accept, edit, revoke, and enforce them on
every future entity save.
Features
AI Validation Generator turns a plain-English description of a field's constraints into enforced Drupal validation.
- Generate validation rules for any fieldable entity type and bundle—Content, Media, Taxonomy term, User, Comment, Custom block, and more.
- Works on base fields (e.g. node Title) as well as configurable fields (e.g. Body, or a custom field).
- Fixed, safe vocabulary of seven rule types: regex, length, numeric range, allowed values, no HTML, word count, and uniqueness. The AI only ever selects a type and fills in its parameters—it never returns, and this module never executes, arbitrary code.
- Optional per-field, plain-English hint steers what the AI suggests (e.g. "must be a US ZIP code").
- Uses structured JSON responses, with automatic retry if the model returns malformed output.
- Works with any AI provider supported by the Drupal AI module, including OpenAI, Anthropic, and others.
Review before enforcing
Nothing is enforced until you review and save the suggested rules.
- Each suggested rule is shown with its type, message, and parameters.
- Uncheck any suggestion you don't want before saving.
- Regenerating a field's rules replaces the previous AI-suggested rules with the fresh batch, while leaving anything you added by hand untouched—so re-running generation never piles up duplicates.
Revoke and restore
Turn enforcement off for a rule set without deleting it, then turn it back on later.
- A one-click Revoke/Restore link next to Edit and Delete on the rule sets list.
- A bulk action: select several rule sets and revoke or restore them all at once.
- Both take effect immediately, the same as saving or deleting a rule set does.
Manual authoring
Rule sets can be created and edited entirely by hand, without ever calling the AI.
- Add a new rule set for any bundle/field that supports validation rules.
- Add, remove, or reorder individual rules within an existing rule set.
- Toggle a single rule—or an entire rule set—off without deleting it.
Configurable AI generation
- AI provider and model selection (or fall back to the site's default chat provider).
- Temperature.
- Maximum output tokens.
- Request rate limits (per-user flood control).
The module automatically applies only the generation parameters supported by the selected AI model.
Typical use cases
- Enforcing consistent formatting on titles, SKUs, codes, or reference numbers.
- Bounding numeric fields (prices, quantities, durations) to a sensible range.
- Requiring a minimum/maximum length or word count on summaries and descriptions.
- Blocking HTML markup in plain-text fields.
- Enforcing uniqueness on fields that should never be duplicated (e.g. a title or code).
- Quickly rolling out validation across many content types without writing custom form validation code.
Post-Installation
After installing the module:
-
Install and configure a chat AI provider from
Configuration → AI → AI Infrastructure → AI Platform Providers. -
Configure AI Validation Generator settings at
Configuration → Development → AI Validation Generator → Settings. -
Configure:
- AI provider and model
- Temperature
- Maximum output tokens
- Generation rate limits
-
Grant the Generate AI Validation Rules permission to roles that should generate and save rules,
and Administer AI Validation Generator to roles allowed to change settings and manually author
rule sets. -
Navigate to
Configuration → Development → AI Validation Generator. - Choose an entity type and bundle, then load its fields.
- Select the fields you want rules for, optionally adding a plain-English hint per field.
- Generate with AI and review the suggested rules.
- Uncheck anything you don't want, then save the selected rules.
-
Manage existing rule sets any time at
Configuration → Development → AI Validation Generator → Rule sets—edit,
revoke/restore, delete, or add one by hand.
Nothing is enforced automatically. Every generated rule is reviewed and explicitly accepted before it can block a
single save.
Additional Requirements
- Drupal 11
- Drupal Field module (core, enabled by default)
- AI module
- At least one configured AI chat provider
- A valid API key for the selected AI provider
No other module is required: any fieldable content entity type already enabled on the site is a valid target.
Supported providers include (but are not limited to):
- AI Provider OpenAI
- AI Provider Anthropic
- Any provider supported by the Drupal AI ecosystem
Recommended modules/libraries
- AI – Provides the AI infrastructure used by this module.
- AI Provider OpenAI – Enables OpenAI models.
- AI Provider Anthropic – Enables Anthropic Claude models.
- Additional AI provider modules supported by the Drupal AI ecosystem.
Similar projects
Several Drupal modules help with field validation or AI-assisted content creation. AI Validation Generator focuses
specifically on AI-suggested, enforced field validation rules.
Key differences include:
- Rules are suggested by AI from a plain-English description, not hand-written by a developer.
- A fixed, safe rule vocabulary—the AI never returns, and the module never executes, arbitrary code.
- Editable, reviewable suggestions before anything is enforced.
- Works on base fields as well as configurable fields.
- One-click and bulk revoke/restore, separate from permanent deletion.
- Provider-independent architecture through the Drupal AI module.
Rather than generating content, structure, or free-form text, AI Validation Generator specializes in generating and
enforcing Drupal field validation constraints.
Supporting this Module
Bug reports, feature requests, patches, and documentation improvements are welcome through the Drupal.org issue queue.
Community contributions, testing, and feedback help improve the module for everyone.
Community Documentation
Documentation is available in the project repository's README.
Future community resources may include:
- Installation guides.
- Configuration tutorials.
- Example prompts and hints.
- YouTube walkthroughs.
- DrupalPod demonstrations.
Community-contributed tutorials and documentation are always welcome.
Permissions
- Generate AI Validation Rules – Allows users to generate, review, and save AI-suggested field validation rules.
- Administer AI Validation Generator – Allows administrators to configure the AI provider/generation settings, and to manually create, edit, revoke, restore, or delete rule sets.
Architecture
- RuleTypeRegistry – Single source of truth mapping each Drupal field type to the rule types applicable to it.
- FieldScanner – Scans fieldable entity types, bundles, and fields for the generator UI.
- PromptBuilder – Builds the AI prompt and its structured JSON schema.
- AiGenerationService – Resolves the configured AI provider, submits chat requests, and decodes the response, retrying once on malformed JSON.
- RuleSetValidator – Validates and normalizes the AI's suggested rules against the fixed rule vocabulary before anything is stored.
- RuleEvaluator – Evaluates the six database-free rule types against a field value.
- RuleSet – The config entity storing one bundle/field's rules; creates a base field override when needed and invalidates the field-definition cache on every save or delete.
- AiValidationRuleSetConstraint / ConstraintValidator – The Symfony validation constraint that enforces a rule set's rules whenever the field is validated.
- GenerateRulesForm – The multi-step select bundle/fields → generate → review → save workflow.
- RuleSetForm – Manual add/edit form for authoring a rule set without AI.
- RuleSetOverviewForm / RuleSetListBuilder – The rule sets list, with per-row and bulk revoke/restore/delete operations.
- SettingsForm – Manages AI provider and generation configuration.
The module is fully compatible with Drupal 11 and works with any AI provider supported by the Drupal AI module.