Serialization & Deserialization

Aurelia's validation system supports serializing and deserializing validation rules to and from JSON. This is particularly useful for scenarios where validation rules are defined on the server and need to be transmitted to the client, or when you need to persist validation rules for later use.

Overview

The validation system provides three main tools for working with serialized rules:

  1. ValidationSerializer - Converts validation rules to JSON strings

  2. ValidationDeserializer - Converts JSON strings back to validation rules

  3. ModelValidationExpressionHydrator - Converts model-based rule definitions (covered in Model-Based Validation)

ValidationSerializer

The ValidationSerializer class converts validation rules and property rules into JSON format that can be transmitted, stored, or logged.

Supported Rule Types

The serializer supports all built-in validation rules:

  • RequiredRule

  • RegexRule (including email)

  • LengthRule (minLength, maxLength)

  • SizeRule (minItems, maxItems)

  • RangeRule (min, max, range, between)

  • EqualsRule

Basic Serialization

Serialized Rule Format

Here's an example of what serialized rules look like:

Serializing Individual Rules

You can serialize individual rules, properties, or property rules:

ValidationDeserializer

The ValidationDeserializer class converts JSON strings back into validation rules that can be used at runtime.

Setup

Before using the deserializer, you must register it with your DI container:

Basic Deserialization

Complete Round-Trip Example

Here's an example showing serialization on one component and deserialization on another:

Server-Side Validation Rules

A common use case is receiving validation rules from a server API:

Limitations and Considerations

Unsupported Rule Types

The following rule types cannot be serialized:

  1. StateRule - State-based validation rules use functions that cannot be serialized to JSON

  2. GroupRule - Group validation rules also use functions

  3. Custom rules with function properties - Any custom validation rule that includes functions

When attempting to serialize these rules in development mode, you'll receive a warning in the console.

Functions and Closures

Since JSON cannot represent functions, any rule that depends on function execution cannot be fully serialized:

Tagged Rules

Rules with tags are fully supported in serialization:

Advanced: Custom Rule Serialization

If you have custom validation rules that you want to serialize, you'll need to extend both the serializer and deserializer.

Extending the Serializer

Extending the Deserializer

Then register your custom deserializer:

Use Cases

1. Storing Validation Rules

Persist validation rules to a database and load them dynamically:

2. Sharing Validation Rules

Share validation rules between client and server to ensure consistency:

3. API-Driven Validation

Build forms dynamically based on API responses that include validation rules:

See Also

Last updated

Was this helpful?