Table of Contents

Class HeroKitRuleBuilderExtensions

Namespace
HeroKit.FluentValidation
Assembly
HeroKit.FluentValidation.dll

FluentValidation rules for HeroKit validators.

public static class HeroKitRuleBuilderExtensions
Inheritance
HeroKitRuleBuilderExtensions
Inherited Members

Remarks

Null and empty values pass, so optional properties stay optional. Combine with NotEmpty() to require a value. Messages use the FluentValidation placeholder {PropertyName} and can be replaced with WithMessage. Country rules take a Country, for example Turkey.

Methods

CardNumber<T>(IRuleBuilder<T, string?>)

Validates that the value is a valid card number.

public static IRuleBuilderOptions<T, string?> CardNumber<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Ettn<T>(IRuleBuilder<T, string?>)

Validates that the value is a valid ETTN (UUID).

public static IRuleBuilderOptions<T, string?> Ettn<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

GibInvoiceNumber<T>(IRuleBuilder<T, string?>)

Validates that the value is a valid e-invoice number (GİB format).

public static IRuleBuilderOptions<T, string?> GibInvoiceNumber<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Gtin<T>(IRuleBuilder<T, string?>)

Validates that the value is a valid GTIN barcode number (EAN-8, UPC-A, EAN-13, GTIN-14).

public static IRuleBuilderOptions<T, string?> Gtin<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Iban<T>(IRuleBuilder<T, string?>)

Validates that the value is a valid IBAN.

public static IRuleBuilderOptions<T, string?> Iban<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

IdCardSerial<T>(IRuleBuilder<T, string?>)

Validates that the value is a Turkish chip identity card serial number.

public static IRuleBuilderOptions<T, string?> IdCardSerial<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

KepAddress<T>(IRuleBuilder<T, string?>)

Validates that the value is a KEP (Kayıtlı Elektronik Posta) address.

public static IRuleBuilderOptions<T, string?> KepAddress<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Mersis<T>(IRuleBuilder<T, string?>)

Validates that the value is a valid MERSİS number.

public static IRuleBuilderOptions<T, string?> Mersis<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

NationalId<T>(IRuleBuilder<T, string?>, Country)

Validates that the value is a valid national identity number of a country.

public static IRuleBuilderOptions<T, string?> NationalId<T>(this IRuleBuilder<T, string?> ruleBuilder, Country country)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

country Country

The country, for example Turkey.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Examples

RuleFor(x => x.Value).NationalId(Countries.Turkey);

PhoneNumber<T>(IRuleBuilder<T, string?>, Country, bool)

Validates that the value is a valid phone number of a country, or a mobile number when mobileOnly is true.

public static IRuleBuilderOptions<T, string?> PhoneNumber<T>(this IRuleBuilder<T, string?> ruleBuilder, Country country, bool mobileOnly = false)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

country Country

The country, for example Turkey.

mobileOnly bool

When true, only mobile numbers pass.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Examples

RuleFor(x => x.Mobile).PhoneNumber(Countries.Turkey, mobileOnly: true);

PostalCode<T>(IRuleBuilder<T, string?>, Country)

Validates that the value is a valid postal code of a country.

public static IRuleBuilderOptions<T, string?> PostalCode<T>(this IRuleBuilder<T, string?> ruleBuilder, Country country)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

country Country

The country, for example Turkey.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Examples

RuleFor(x => x.Value).PostalCode(Countries.Turkey);

SafeUpload<T, TFile>(IRuleBuilder<T, TFile?>, Func<TFile, Stream>, Func<TFile, string>, UploadPolicy?)

Validates an uploaded file with UploadValidator: file name, program or script content, content that does not fit the extension, macros, active SVG content, size and allowed types, as set in policy.

public static IRuleBuilderOptions<T, TFile?> SafeUpload<T, TFile>(this IRuleBuilder<T, TFile?> ruleBuilder, Func<TFile, Stream> openRead, Func<TFile, string> fileName, UploadPolicy? policy = null) where TFile : class

Parameters

ruleBuilder IRuleBuilder<T, TFile>

The rule builder.

openRead Func<TFile, Stream>

Opens the file content for reading.

fileName Func<TFile, string>

Returns the file name as uploaded.

policy UploadPolicy

The upload policy; null uses Default.

Returns

IRuleBuilderOptions<T, TFile>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

TFile

The file type.

Examples

var imagesOnly = new UploadPolicy { AllowedTypes = [FileTypes.Jpeg, FileTypes.Png], MaxLength = 5 * 1024 * 1024 };
RuleFor(x => x.Photo).NotNull().SafeUpload(f => f.OpenReadStream(), f => f.FileName, imagesOnly);

Remarks

Works with any file type: give a function that opens the content and one that returns the file name, for example for ASP.NET Core's IFormFile or Blazor's IBrowserFile. The rule opens the stream, reads what the check needs and disposes it. A null file passes; add NotNull() to require one. The message is '{PropertyName}' is not accepted: {UploadMessage}, where {UploadMessage} is Message; replace it with WithMessage.

TaxId<T>(IRuleBuilder<T, string?>, Country)

Validates that the value is a valid tax identifier of a country.

public static IRuleBuilderOptions<T, string?> TaxId<T>(this IRuleBuilder<T, string?> ruleBuilder, Country country)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

country Country

The country, for example Turkey.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Examples

RuleFor(x => x.Value).TaxId(Countries.Turkey);

Tckn<T>(IRuleBuilder<T, string?>)

Validates that the value is a valid Turkish identity number (TCKN).

public static IRuleBuilderOptions<T, string?> Tckn<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

VehiclePlate<T>(IRuleBuilder<T, string?>, Country)

Validates that the value is a valid vehicle plate of a country.

public static IRuleBuilderOptions<T, string?> VehiclePlate<T>(this IRuleBuilder<T, string?> ruleBuilder, Country country)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

country Country

The country, for example Turkey.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.

Examples

RuleFor(x => x.Value).VehiclePlate(Countries.Turkey);

Vkn<T>(IRuleBuilder<T, string?>)

Validates that the value is a valid Turkish tax number (VKN).

public static IRuleBuilderOptions<T, string?> Vkn<T>(this IRuleBuilder<T, string?> ruleBuilder)

Parameters

ruleBuilder IRuleBuilder<T, string>

The rule builder.

Returns

IRuleBuilderOptions<T, string>

The rule builder options, for chaining.

Type Parameters

T

The validated object type.