Types of Validators
Safe Settings supports two types of custom validators:Config Validators
Validate a setting on its own, regardless of inheritance or overrides. These ensure that settings meet your organization’s requirements at any level. Use case: Prevent collaborators from being assigned admin permissionsOverride Validators
Validate settings when a lower-level configuration (suborg or repo) attempts to override a higher-level setting (org or suborg). These maintain minimum security standards across your organization. Use case: Ensure branch protection requirements can only be made more restrictive, never lessConfiguration Location
Custom validators are configured indeployment-settings.yml in the directory where Safe Settings runs. This file is separate from your repository settings and controls the application’s behavior.
Config Validators
Config validators check individual settings to ensure they meet your policies.Structure
Available Variables
baseconfig- The complete setting being validatedconsole.log()- Available for debugging (outputs to application logs)
Example: Prevent Admin Collaborators
Example: Enforce Label Naming Convention
deployment-settings.yml
Example: Require Specific Team Permissions
deployment-settings.yml
Override Validators
Override validators ensure that settings can only be overridden in specific ways, maintaining minimum standards.Structure
Available Variables
baseconfig- The higher-level setting (org or suborg)overrideconfig- The lower-level setting attempting to override (suborg or repo)console.log()- Available for debugging
Example: Maintain Minimum Approvals
Prevent repos from reducing the required number of approvals below the org-level setting.Example: Enforce Status Check Requirements
deployment-settings.yml
Example: Maintain Enforce Admins Setting
deployment-settings.yml
Complete Example
Validation Behavior
When Validators Run
- Pull Request Checks: Validators run in dry-run mode when settings changes are proposed
- Settings Application: Validators run when applying settings to repositories
- Merge Operations: During configuration merging across org, suborg, and repo levels
Validation Failure
When a validator fails:- In PR checks: The check fails with the error message, preventing merge
- During sync: An error is logged and the operation stops
- Error details: The custom error message is shown in logs and check runs
Check Run Example
When validation fails in a pull request:Best Practices
Write Defensive Code
Always check for the existence of nested properties before accessing them:Use Clear Error Messages
Error messages should:- Explain what policy was violated
- Provide guidance on how to fix the issue
- Include relevant configuration values when helpful
Test Validators Thoroughly
Before deploying validators:- Test with various configuration combinations
- Verify error messages are clear and actionable
- Use
console.log()during development to debug - Test both valid and invalid scenarios
Return Early
Returntrue early for cases that don’t need validation:
Document Your Validators
Maintain documentation of your custom validators:- What policy each validator enforces
- Why the policy exists
- Who to contact for exceptions
- Examples of valid and invalid configurations
Debugging Validators
View Validator Output
Validatorconsole.log() output appears in Safe Settings application logs:
Test in Pull Requests
- Create a test branch in your admin repo
- Modify settings to trigger validation
- Open a PR to see validator results
- Check the dry-run output in the PR check
Common Issues
Validator always fails- Check property names (case-sensitive)
- Verify the structure of baseconfig/overrideconfig
- Add console.log() to debug values
- Verify the plugin name matches the setting name
- Check deployment-settings.yml syntax
- Ensure the file is in the correct location
- JavaScript syntax must be valid
- Return statement is required
- Variables must be declared with let/const
Advanced Patterns
Conditional Validation by Repository
Multi-Field Validation
Environment-Specific Rules
While validators can’t directly access environment variables, you can structure your logic based on configuration patterns:Plugin Names Reference
Validatorplugin field must match these setting types:
| Plugin Name | Setting Type |
|---|---|
collaborators | External collaborator permissions |
teams | Team permissions |
branches | Branch protection rules |
labels | Issue/PR labels |
milestones | Repository milestones |
repository | Repository settings |
rulesets | Repository rulesets |
environments | Deployment environments |
autolinks | Autolink references |
custom_properties | Custom properties |
variables | Variables |