Schema Validation Validate schema correctness before runtime
VibORM includes a comprehensive validation system that checks your schema for errors before runtime.
import { validateSchemaOrThrow } from "viborm" ;
// Validate all models
validateSchemaOrThrow ([user, post, comment]);
Returns a validation result with all errors:
import { validateSchema } from "viborm" ;
const result = validateSchema ([user, post]);
if ( ! result.valid) {
for ( const error of result.errors) {
console. log ( `[${ error . code }] ${ error . model }: ${ error . message }` );
}
}
Throws on the first error (useful for startup):
import { validateSchemaOrThrow } from "viborm" ;
// Throws if invalid
validateSchemaOrThrow ([user, post]);
For custom validation:
import { SchemaValidator } from "viborm" ;
const validator = new SchemaValidator ([user, post], {
database: "postgres" , // Optional: enable DB-specific rules
});
const result = validator. validate ();
Code Category Description M0xx Model Basic model structure F0xx Field Field-level constraints I0xx Index Index definitions R0xx Relation Relation configuration JT0xx Junction Many-to-many tables SR0xx Self-ref Self-referential relations CM0xx Cross-model Cross-model dependencies FK0xx Foreign Key FK field validation RA0xx Referential Action onDelete/onUpdate rules DB0xx Database Database-specific constraints
Code Rule Description M001 modelHasFieldsModel must have at least one field M002 modelUniqueNameModel names must be unique M003 modelNameValidModel name cannot be empty M004 modelNameNotReservedModel name cannot be SQL reserved word
Code Rule Description F001 noduplicateFieldsNo duplicate field names F002 modelHasIdModel must have exactly one ID (field or compound) F003 defaultTypeMatchDefault value must match field type F004 arrayFieldsSupportedArray fields only on supported DBs
Code Rule Description I001 indexFieldsExistIndex fields must exist in model I002 indexNameUniqueIndex names must be unique per model I003 compoundFieldsExistCompound ID/unique fields must exist
Code Rule Description R001 relationTargetExistsRelation target model must exist R002 relationHasInverseBidirectional relations should have inverse R003 relationNameUniqueRelation names must be unique per model
Code Rule Description FK001 fkFieldExistsFK field must exist in model FK002 fkReferenceExistsReferenced field must exist in target FK003 fkTypeMatchFK and reference types must match FK004 fkRequiredForOwningOwning side must have FK FK005 fkReferencesUniqueFK must reference unique field FK006 fkFieldNotRelationFK field cannot be a relation FK007 fkCardinalityMatchFK count must match relation cardinality
Code Rule Description RA001 onDeleteValidonDelete action must be valid RA002 onUpdateValidonUpdate action must be valid RA003 cascadeOnRequiredWarningWarning: CASCADE on required relation RA004 setNullRequiresNullableSET NULL requires nullable FK
Code Rule Description JT001 junctionTableUniqueJunction table names must be unique JT002 junctionFieldsValidJunction FK columns must be valid JT003 junctionFieldsDistinctA and B columns must be different JT004 selfRefJunctionOrderSelf-ref junction must have consistent order JT005 throughOnlyManyToMany.through() only on many-to-many
Code Rule Description SR001 selfRefValidInverseSelf-ref must have valid inverse SR002 selfRefDistinctNamesSelf-ref relations must have distinct names
Code Rule Description CM001 noOrphanFkFieldsFK fields should have relation CM002 relationPairFkSingleSideOnly one side should have FK CM003 polymorphicRelationWarningWarning for polymorphic pattern CM004 noCircularRequiredChainNo circular required relations
Code Rule Description DB001 mysqlNoArrayFieldsMySQL doesn't support array fields DB002 sqliteNoEnumSQLite enum is stored as TEXT
interface ValidationError {
code : string ; // e.g., "F002"
severity : "error" | "warning" ;
model : string ; // Model name
field ?: string ; // Field name (if applicable)
message : string ; // Human-readable message
}
interface ValidationResult {
valid : boolean ;
errors : ValidationError [];
}
const result = validateSchema ([user, post]);
// Example errors:
// [F002] User: Model must have exactly one ID field
// [FK003] Post.authorId: FK type 'int' doesn't match reference type 'string'
// [RA004] Comment.postId: SET NULL requires nullable FK field
Validate on startup — Catch errors early
Use validateSchemaOrThrow — Fail fast in development
Check warnings — Warnings may indicate issues
Database-specific validation — Pass database option for DB rules