Schema validation
MD-Models provides comprehensive validation capabilities to ensure your data models are well-formed, consistent, and ready for code generation. The validation system checks for structural integrity, naming conventions, type consistency, and other potential issues that could cause problems during code generation or runtime.
The Validate Command
The validate command checks your MD-Models file for errors and inconsistencies. It can validate both markdown model files and JSON Schema files.
Basic Usage
md-models validate -i <input>
Examples:
# Validate a local markdown model file
md-models validate -i model.md
# Validate a model from a remote URL
md-models validate -i https://example.com/model.md
# Validate a JSON Schema file
md-models validate -i schema.json
Input Sources
The validate command accepts the same input types as the convert command:
- Local file path: Path to a markdown model or JSON Schema file on your local filesystem
- Remote URL: URL to a markdown model or JSON Schema file hosted online
MD-Models automatically detects the file type (markdown model vs JSON Schema) and applies the appropriate validation rules.
Validation Checks
MD-Models performs comprehensive validation checks on your data model. Understanding these checks helps you write correct models and quickly identify issues.
Global Model Validation
Empty Model Check
What it checks: Ensures your model contains at least one object definition.
Error message: "This model has no definitions."
Solution: Add at least one object to your model.
Example:
# My Model
<!-- This would fail validation - no objects defined -->
Duplicate Object Names
What it checks: Ensures each object has a unique name within the model.
Error type: DuplicateError
Error message: "Object '<name>' is defined more than once."
Solution: Rename one of the duplicate objects to be unique.
Example:
## User
- name: string
## User <!-- Error: duplicate name -->
- email: string
Duplicate Enum Names
What it checks: Ensures each enumeration has a unique name within the model.
Error type: DuplicateError
Error message: "Enumeration '<name>' is defined more than once."
Solution: Rename one of the duplicate enumerations to be unique.
Object Validation
Empty Object Check
What it checks: Ensures objects have at least one attribute (unless allow_empty: true is set in frontmatter).
Error type: ObjectError
Error message: "Type '<name>' is empty and has no properties."
Solution: Add at least one property to the object, or set allow_empty: true in the model’s frontmatter if empty objects are intentional.
Example:
## EmptyObject
<!-- This would fail validation - no attributes -->
Object Name Validation
What it checks: Validates that object names follow naming conventions:
- Must start with a letter: Object names cannot start with numbers or special characters
- No whitespace: Object names cannot contain spaces
- No special characters: Only alphanumeric characters and underscores are allowed
Error type: NameError
Error messages:
"Name '<name>' must start with a letter.""Name '<name>' contains whitespace, which is not valid. Use underscores instead.""Name '<name>' contains special characters, which are not valid except for underscores."
Valid examples:
UserUserProfileuser_profileUser123
Invalid examples:
123User(starts with number)User Profile(contains space)User-Profile(contains special character)User.Profile(contains special character)
Attribute Validation
Duplicate Attribute Names
What it checks: Ensures each attribute within an object has a unique name.
Error type: DuplicateError
Error message: "Property '<name>' is defined more than once."
Solution: Rename one of the duplicate properties to be unique within the object.
Example:
## User
- name: string
- name: string <!-- Error: duplicate attribute name -->
Attribute Name Validation
What it checks: Validates that attribute names follow the same naming conventions as object names:
- Must start with a letter
- No whitespace
- No special characters (except underscores)
Error type: NameError
Error messages: Same as object name validation
Valid examples:
nameuser_nameemailAddressage123
Invalid examples:
123age(starts with number)user name(contains space)user-name(contains special character)
Type Definition Validation
What it checks: Ensures every attribute has at least one valid type defined.
Error type: TypeError
Error messages:
"Property '<name>' has no type specified.""Property '<name>' has no type defined. Either define a type or use a base type."
Solution: Add a type to the property using the syntax - <property>: <TYPE>.
Example:
## User
- name <!-- Error: no type specified -->
- email: string <!-- Valid -->
Type Reference Validation
What it checks: Ensures that all referenced types exist in the model or are basic types.
Error type: TypeError
Error message: "Type '<type>' of property '<name>' not found."
Solution: Either add the referenced type to your model, or use one of the basic types: string, number, integer, boolean, float, date, bytes.
Valid basic types:
string- Text datanumber- Numeric valueinteger- Whole numberboolean- True/false valuefloat- Floating-point numberdate- Date valuebytes- Binary data
Example:
## User
- name: string <!-- Valid: basic type -->
- profile: UserProfile <!-- Error if UserProfile doesn't exist -->
- status: Status <!-- Valid if Status enum exists -->
XML Option Validation
MD-Models validates XML serialization options to ensure they’re correctly formatted.
XML Element Option Validation
What it checks: Validates XML element options (used for custom XML tag names).
Error type: XMLError
Error messages:
"XML option is not defined.""Name '<name>' contains special characters..."
Solution: Ensure XML options are properly defined and don’t contain invalid characters.
XML Attribute Option Validation
What it checks: Validates XML attribute options (used for XML attributes).
Error type: XMLError
Error messages:
"XML attribute option is not defined.""Name '<name>' contains special characters..."
Solution: Ensure XML attribute options use the @ prefix and are properly formatted.
XML Wrapped Option Validation
What it checks: Validates XML wrapped element options (for nested XML structures).
Error type: XMLError
Error messages:
"XML wrapped option can only contain two types.""Name '<name>' contains special characters..."
Solution: XML wrapped options can only have a depth of two types. For deeper nesting, create intermediate objects.
Error Reporting
When validation fails, MD-Models provides detailed error messages to help you identify and fix issues.
Error Format
Each validation error includes:
- Line number: The line(s) where the error occurs
- Location: The object and attribute (if applicable) where the error was found
- Error type: The category of error (NameError, TypeError, DuplicateError, etc.)
- Message: A clear description of what’s wrong
- Solution: Suggested fix for the error
Example Error Output
[line: 5, 12] [User.name] NameError:
└── Name 'user name' contains whitespace, which is not valid. Use underscores instead.
Resolve the issue by using 'user_name'.
[line: 8] [User] TypeError:
└── Type 'Profile' of property 'profile' not found.
Add the type 'Profile' to the model or use a base type.
Error Types
MD-Models categorizes errors into several types:
NameError: Issues with naming conventions (object names, attribute names)TypeError: Issues with type definitions or type referencesDuplicateError: Duplicate definitions (objects, enums, attributes)ObjectError: Issues with object structure (empty objects)XMLError: Issues with XML serialization optionsGlobalError: Model-level issues (empty model)
Validation Success
When validation passes, you’ll see:
✓ Model is valid
This indicates your model is well-formed and ready for code generation or export.
JSON Schema Validation
MD-Models can also validate JSON Schema files. When you provide a JSON Schema file as input, MD-Models:
- Parses the JSON Schema
- Converts it to an internal model representation
- Validates the converted model using the same validation rules
This allows you to validate JSON Schema files and ensure they’re compatible with MD-Models workflows.
Example:
md-models validate -i schema.json
Best Practices
- Validate before generating: Always validate your model before running code generation to catch errors early
- Fix errors systematically: Start with global errors (duplicates, empty model) before fixing attribute-level errors
- Use descriptive names: Follow naming conventions to avoid NameError issues
- Check type references: Ensure all referenced types exist in your model or are basic types
- Validate in CI/CD: Include validation checks in your continuous integration pipeline
Integration with Code Generation
Validation is automatically performed when parsing models for code generation. If validation fails during parsing, code generation will not proceed, ensuring that only valid models are used to generate code.
You can also use validation independently to check models without generating code:
# Just validate
md-models validate -i model.md
# Validate and generate (validation happens automatically)
md-models convert -i model.md -t python-pydantic -o models.py