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.

Validation attributes belong on entities and on DTOs that accept input (request bodies you validate and persist). Readonly / output-only DTOs don't need them — generated validation never runs against data the server itself produced. The exception is [Required], which also drives Swagger nullability and the generated TypeScript types, so keep it on output DTOs.

Custom Validation Rules

For validation logic that can't be expressed with attributes, use the ConfigureCustomRules() partial method. A rule that needs the database or the network (a value another row holds, for example) goes in the entity service's Validate{Entity}Async instead: see Save Operation Hooks. 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.

An address with two dots in a row (a..b@example.com, a@example..com) is refused. From .NET 10, System.Net.Mail.MailAddress throws on such an address, so a mail to it can never go. The built-in login and verification-code requests carry [Email] too: an account saved earlier with such an address gets a validation error there, in place of a failed mail.

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. It means what ASP.NET Core's RequiredAttribute means: it refuses null.

  • On a string, it also refuses "" and a whitespace string.
  • On a navigation, its {Nav}Id also refuses 0, because no row has that id.
  • On any other type (a bool, a number, a date, an enum, a list), it refuses only null. false, 0, a date and [] are legal answers. A foreign key without a navigation is a number here.

Spiderly does not read AllowEmptyStrings, so a [Required] string always refuses "".

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

Where false, 0 or [] is not a legal answer, add a rule for it: [Range] or [GreaterThanOrEqualTo] on a number, else a custom rule.

Example:

public class Order : BusinessObject<long>
{
    [Required] // Cannot be null, empty or whitespace
    [StringLength(100, MinimumLength = 1)]
    public string CustomerName { get; set; }

    [Required] // 0 is a legal answer
    public int DiscountPercent { get; set; }

    [Required]
    [Range(1, 100)] // 0 is not a legal answer, so Range refuses it
    public int Quantity { get; set; }
}

A list that must not be empty takes a custom rule on the server:

partial void ConfigureCustomRules()
{
    RuleFor(x => x.Items).NotEmpty();
}

On the client, call isFormArrayEmpty from your ValidatorService. The generated setFormArrayValidator is an arrow-function property, so override it as one:

override setFormArrayValidator = (formArray: SpiderlyFormArray, className: string): void => {
  if (formArray.label + className === 'itemsOrder') this.isFormArrayEmpty(formArray);
};

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 entity string properties. If omitted, EF Core maps them to NVARCHAR(MAX), which can hurt performance. This is a schema concern, so it applies to entity columns only — output/readonly DTO strings don't need it (see the note at the top of this page).

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.

Entry semantics:

  • MIME entries ("image/png") are enforced server-side (declared-type whitelist + content inspection) and passed to the UI file picker.
  • Type wildcards ("image/*") match any declared type with that prefix.
  • Extension entries (".pdf" — leading dot, no /) only widen the UI file picker; always pair them with the matching MIME type.
  • "image/svg+xml" is validated structurally (XML with an <svg> root; active content like scripts and event-handler attributes is rejected) since SVG has no magic bytes, and it skips the image optimization — the file uploads as-is.

A refused file answers 422 validation_failed, with the specific localized message under /file in fieldErrors, so the user sees it rather than a generic error. The File Storage page shows the upload flow.

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; }
}