Problem / Motivation
ai_validations currently ships an AI Image Classification field validation rule that runs an uploaded
image through an AI provider's image-classification operation and validates the field based on whether
a configured tag is returned above a minimum confidence threshold.
There is no equivalent for text. We should add an AI Text Classification validation rule that does the
same for text/string/long-text fields: send the field value to an AI provider's classification
operation and validate based on the returned label and confidence. Useful for rejecting
spam/toxic/off-topic submissions, requiring a field to fall into an allowed category, or flagging
sentiment.
Proposed resolution
Mirror the existing image classification implementation, swapping the image input/operation for a text
classification input/operation. Three new classes, modeled on their image counterparts:
1. src/Plugin/FieldValidationRule/AiTextClassificationConstraintFieldValidationRule.php — model on the
image version. getConstraintName() returns AiTextClassification. Same config form: model selection
(limited to text-classification-capable models), tag, finder method (exact/contains/case-insensitive),
minimum confidence, "model not available" behavior, custom message.
2. src/Plugin/Validation/Constraint/AiTextClassificationConstraint.php — model on the image version.
@Constraint id AiTextClassification, label "AI Text Classification Check", same properties (message,
model, tag, finder, minimum, na).
3. src/Plugin/Validation/Constraint/AiTextClassificationConstraintValidator.php — model on the image
version. Instead of loading a file and calling $provider->imageClassification(...), take the text
value directly and call the AI module's text classification operation, then apply the same finder +
minimum-confidence matching.
Reuse the matching logic, confidence comparison, na exception handling, and DI structure from the
image version so behaviour stays consistent.
Remaining tasks
- [ ] Confirm the AI module exposes a text classification operation/input class (analogous to
imageClassification() / ImageClassificationInput)
- [ ] Add the three plugin classes
- [ ] Restrict the model dropdown to text-classification-capable providers
- [ ] Add test coverage mirroring the image classification tests
- [ ] Update docs/README
User interface changes
A new AI Text Classification option in the field validation rule list, with a config form matching the
image classification rule.
Issue fork ai_validations-3595515
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
marcus_johansson commentedComment #5
abhisekmazumdarSteps to test
ai_validationsand configure at least one AI provider that supportstext_classification.spam.0.8.Comment #6
abhisekmazumdarWhat was done
I have added the AI text classification constraint as a mirror of the existing image classification constraint. The MR adds three plugin files and a Kernel test suite.
Differences from the original plan
Validator-logic test: Unit test was not viable
The issue summary asked for test coverage mirroring the image classification tests. The image classification feature ships no validator-logic test itself, only a FunctionalJavascript form test.
I tried a Unit test first.
AiProviderPluginManageris declaredfinal, so PHPUnit cannot mock it. A Unit test covering the full call chain requires bypass-finals, which the module does not use.I wrote a Kernel test instead, backed by
ai_validations_test. That module registers aClassifierTestProviderextendingEchoProviderwhosetextClassification()output is controlled via Drupal state. The Kernel test drives every branch through the real provider manager.EchoProvider cannot trigger a positive match
EchoProviderreturns labels only when the input carries them. The validator passesnew TextClassificationInput($text)with no labels, soEchoProvideralways returns an empty list.ClassifierTestProviderinai_validations_testwas added to work around this.Plugin id typo not copied
The image classification plugin id has a space:
ai_image_classification constraint_rule. The new plugin usesai_text_classification_constraint_rulewith no space. This is intentional.Comment #7
abhisekmazumdarFound and fixed a bug while preping for demo
When a site has no
text_classificationprovider configured and the rule is set to na: skip, the validator was still showing an error message on save instead of silently passing. Thenasetting was being ignored at the provider lookup stage.The fix adds a parameter to
resolveProviderOption()so callers can tell it not to raise a violation when no provider is found. The text classification validator now passes that flag based on thenasetting.Also updated one kernel test that was affected by the change, and corrected a minor code style issue flagged by the CI pipeline.
Three commits have been pushed to the MR branch.
Comment #8
marcus_johansson commentedSetting to needs work, but really just a NIT. Feel free to merge without fixing.
Comment #9
marcus_johansson commentedComment #10
marcus_johansson commentedComment #11
abhisekmazumdarComment #17
abhisekmazumdarComment #19
arianraeesi commented