Permission Mongo provides comprehensive schema validation to ensure data integrity and consistency across your collections.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/KTS-o7/permission-mongo/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The schema validation system:- Type checking - Enforce field types (string, number, date, objectId, etc.)
- Constraints - Define min/max lengths, patterns, enums, and ranges
- Required fields - Ensure critical fields are always present
- Immutable fields - Prevent modification of fields after creation
- Nested validation - Validate objects and arrays recursively
- Custom formats - Email, URL, and other format validation
Schema definition
Define schemas inschema.yml:
schema.yml
Field types
Permission Mongo supports these field types:Text values with optional length and pattern constraints
Floating-point numbers with optional min/max constraints
Whole numbers only, with optional min/max constraints
true or false valuesISO8601 date/time values or native time.Time objects
MongoDB ObjectID (24 hex characters)
UUID string in standard format
Array of items with optional item schema
Nested object with property definitions
Any type - no validation applied
String constraints
Validate string fields with these constraints:schema.yml
Minimum string length (inclusive)
Maximum string length (inclusive)
Regular expression pattern the value must match
Predefined format:
email or urlList of allowed values
Default value if field is not provided
Number constraints
Validate numeric fields:schema.yml
Minimum value (inclusive)
Maximum value (inclusive)
Array validation
Validate arrays and their items:schema.yml
Schema for array items. All items must match this schema.
Object validation
Validate nested objects:schema.yml
Map of property names to field schemas. Each property is validated according to its schema.
Field modifiers
Control field behavior with modifiers:schema.yml
Field must be present in documents
Field cannot be modified after creation
Field is automatically generated on document creation
Field is automatically updated on every modification
Field is computed by hooks or application logic - skipped during validation
Validation process
The validator checks documents in this order:- Required field check - Ensure all required fields are present
- Type validation - Verify each field matches its declared type
- Constraint validation - Check min/max, patterns, enums, etc.
- Nested validation - Recursively validate objects and arrays
Validation errors
Validation errors include specific error codes:Field type doesn’t match schema
Required field is missing
String is too short
String is too long
String doesn’t match pattern
String doesn’t match format (email/url)
Number is below minimum
Number exceeds maximum
Value not in allowed enum list
Attempt to modify immutable field
Update validation
When updating documents, the validator checks:- Document validation - Validate the new document
- Immutable fields - Ensure immutable fields haven’t changed
Partial validation
For partial updates (PATCH operations), use partial validation:Example schema
Complete example for a documents collection:schema.yml
Best practices
Always require critical fields
Always require critical fields
Mark essential fields like
tenant_id, owner_id, and identifiers as required: true.Make audit fields immutable
Make audit fields immutable
Set
created_at, created_by, and other audit fields as immutable: true to preserve history.Use enums for known values
Use enums for known values
Define
enum constraints for status fields, types, and other fields with fixed options.Validate formats for external data
Validate formats for external data
Use
format: email and format: url for user-provided data to ensure validity.Set reasonable length limits
Set reasonable length limits
Apply
max_length to prevent abuse and ensure database performance.Related resources
- RBAC - Control access with role-based policies
- Hooks - Execute validation logic in pre-operation hooks
- Versioning - Track document changes over time