Validation

Learn how to use validation attributes to automatically generate backend and frontend validation rules.

Overview

Spiderly simplifies validation across your application by allowing you to annotate your entity classes with attributes.

Validation attributes fall into two categories:

  • Spiderly custom validation attributes
  • EF Core attributes from System.ComponentModel.DataAnnotations

Validation attributes are used to:

  • Automatically generate backend (DTO-level) validation.
  • Automatically generate frontend form validation rules.
  • Leave database validation to EF Core.

Behavior Based on Attribute Type:

  • EF Core attributes will result in database schema changes. When you use them, remember to run database migration and update.
  • Spiderly backend validation attributes require rebuilding the backend to take effect.

Custom Validation Rules

For validation logic that can't be expressed with attributes, use the ConfigureCustomRules() partial method. Spiderly generates each validator as a partial class with an empty ConfigureCustomRules() method. You implement this method in a separate file to add custom FluentValidation rules.

Example:

// Generated by Spiderly (do not edit):
public partial class ProductDTOValidationRules : AbstractValidator<ProductDTO>
{
    public ProductDTOValidationRules()
    {
        RuleFor(x => x.Name).NotEmpty().Length(2, 100);
        ConfigureCustomRules();
    }

    partial void ConfigureCustomRules();
}
// Your hand-written file (e.g., ProductDTOValidationRules.cs):
using FluentValidation;

public partial class ProductDTOValidationRules
{
    partial void ConfigureCustomRules()
    {
        RuleFor(x => x.Slug).Matches(@"^[^/]+$");
    }
}

GreaterThanOrEqualTo

Namespace: Spiderly.Shared.Attributes.Entity

Usage

Validates that a numeric property value is greater than or equal to a specified number. This attribute provides both server-side and client-side validation.

Example:

public class Product : BusinessObject<long>
{
    [GreaterThanOrEqualTo(0)] // StockQuantity must be 0 or higher
    public int StockQuantity { get; set; }
}

Email

Namespace: Spiderly.Shared.Attributes.Entity

Usage

Validates that a string property value is a valid email address. This attribute provides both server-side and client-side validation.

Example:

public class User : BusinessObject<long>
{
    [Email]
    [StringLength(70, MinimumLength = 5)]
    [Required]
    public string Email { get; set; }
}

Precision

Usage

Specifies the precision and scale for a decimal property. Useful for controlling how many digits are stored in total (precision) and how many of them are after the decimal point (scale).

Applies to the server-side, client-side validation and affects the EF Core model and will generate corresponding SQL column definition.

Example:

public class Product : BusinessObject<long>
{
    [Precision(18, 2)] // Allows up to 18 digits total, with 2 after the decimal point
    public decimal Price { get; set; }
}

Range

Usage

Validates that a numeric value falls within a specified inclusive range.

Applies to the server-side, client-side validation and affects the EF Core model and will generate corresponding SQL column definition.

Example:

public class Product : BusinessObject<long>
{
    [Range(1, 1000)] // Price must be between 1 and 1000 (inclusive)
    public decimal Price { get; set; }
}

Required

Usage

Specifies that a value must be provided for the decorated property, enforcing non-null values.

Applies to the server-side, client-side validation and affects the EF Core model and will generate corresponding SQL column definition.

Example:

public class User : BusinessObject<long>
{
    [Required] // Name cannot be null or empty
    public string Name { get; set; }
}

StringLength

Specifies the maximum length, a length range, or an exact length for a string property.

This attribute is already built in EF Core, but apart from that, Spiderly also uses it to generate validations (Backend and Frontend). The behavior aligns with standard .NET semantics — [StringLength(X)] means max-length (minimum defaults to 0).

Applies to the server-side, client-side validation and affects the EF Core model and will generate corresponding SQL column definition.

Always apply StringLength to string properties. If omitted, EF Core will map them to NVARCHAR(MAX) by default, which can negatively impact performance.

Usage

  • [StringLength(100)] without MinimumLength generates a max-length validation (string must have at most 100 characters).
  • [StringLength(100, MinimumLength = 1)] generates a range validation (string must be between 1 and 100 characters).
  • [StringLength(8, MinimumLength = 8)] (min == max) generates an exact-length validation (string must be exactly 8 characters).

Examples:

public class User : BusinessObject<long>
{
    [StringLength(50, MinimumLength = 2)] // Name must be between 2 and 50 characters
    public string Name { get; set; }

    [StringLength(200)] // Description can be up to 200 characters
    public string Description { get; set; }

    [StringLength(6, MinimumLength = 6)] // Code must be exactly 6 characters
    public string Code { get; set; }
}

AcceptedFileTypes

Namespace: Spiderly.Shared.Attributes.Entity

Usage

Specifies the accepted file types for a blob property. This attribute is required on every blob property (any property decorated with a StorageAttribute subclass like [S3PublicStorage], [S3PrivateStorage], [DiskStorage], or a custom one) and must declare at least one MIME-typed value. If it is missing (or contains only extension values like ".pdf" with no image/jpeg-shaped entry), the source generator raises build error SPIDERLY014. There is no implicit default — every blob property must whitelist its accepted types explicitly.

Example:

public class Catalog : BusinessObject<int>
{
    [S3PublicStorage]
    [AcceptedFileTypes("application/pdf", ".pdf")]
    [StringLength(1000, MinimumLength = 1)]
    public string File { get; set; }
}

MaxFileSize

Namespace: Spiderly.Shared.Attributes.Entity

Usage

Specifies the maximum allowed file size (in bytes) for a blob property. When not applied, the file upload defaults to 20 MB (20,000,000 bytes). Use this attribute alongside any StorageAttribute subclass for file uploads.

Example:

public class Catalog : BusinessObject<int>
{
    [S3PublicStorage]
    [MaxFileSize(5_000_000)] // 5 MB
    [AcceptedFileTypes("application/pdf", ".pdf")]
    [StringLength(1000, MinimumLength = 1)]
    public string File { get; set; }
}

ImageWidth

Namespace: Spiderly.Shared.Attributes.Entity

Usage

Validates exact image width (in pixels) for a blob property. This attribute provides both server-side and client-side validation. Use this attribute alongside any StorageAttribute subclass for image uploads.

Example:

public class User : BusinessObject<long>
{
    [S3PublicStorage]
    [ImageWidth(100)]
    [ImageHeight(100)]
    [StringLength(80, MinimumLength = 30)]
    public string Avatar { get; set; }
}

ImageHeight

Namespace: Spiderly.Shared.Attributes.Entity

Usage

Validates exact image height (in pixels) for a blob property. This attribute provides both server-side and client-side validation. Use this attribute alongside any StorageAttribute subclass for image uploads.

Example:

public class User : BusinessObject<long>
{
    [S3PublicStorage]
    [ImageWidth(100)]
    [ImageHeight(100)]
    [StringLength(80, MinimumLength = 30)]
    public string Avatar { get; set; }
}