---
title: Data annotations
description: AutoFixture honors System.ComponentModel.DataAnnotations constraints when generating strings.
---

When properties use validation attributes, generators respect them.

## Example scenario

`ValidatedProfile` has `[Required]` and `[StringLength]` constraints on its properties. Your test needs specimens that satisfy those rules so validation logic is not the thing being tested.

## Example

```csharp
var fixture = new Fixture();

var profile = fixture.Create<ValidatedProfile>();

Assert.False(string.IsNullOrEmpty(profile.Name));
Assert.InRange(profile.Code.Length, 1, ValidatedProfile.CodeMaxLength);
```

## How it works

Supported attributes include `[Required]`, `[StringLength]`, `[Range]`, and others handled by AutoFixture's data annotation relays.

Generated values stay within declared constraints so tests focus on behavior, not invalid input.

## Next steps

- [Fixture and Create](/docs/fundamentals/fixture-and-create)
- [Build DSL](/docs/fundamentals/build-dsl)
- [Collections](/docs/how-to/collections)
- [Cheat sheet](/docs/reference/cheat-sheet)

## API

- [DataAnnotations namespace](/api/autofixture/5-0-0-rc-1/autofixture.dataannotations)
