Repository F# setup
open Reified
open Reified.Refinements
open Reified.Result
open Reified.Schema.Json

Derivation Attributes

Open Reified.DerivedSchema to use the attributes without suffixes:

open Reified.DerivedSchema

Attributes are read from F# source at generation time. They do not use runtime reflection. Each generated operation in the table has the same meaning as its linked handwritten Schema DSL equivalent.

Type and construction attributes

Attribute Applies to Short description Schema DSL equivalent
[<DeriveSchema>] record Generate a schema and companion operations for a public wire record. Chain and Version optionally identify a version series. schema<'model> { ... }
[<DeriveUnion>] union Derive the recommended internally tagged union with discriminator type. Fieldless and directly named payload cases may be mixed. Schema.union
[<DeriveUnion "kind">] union Derive the same named-field format with a custom discriminator. Schema.unionWith
[<DeriveUnion(Representation = UnionRepresentationKind.Adjacent, PayloadField = "Fields", PayloadStyle = UnionPayloadStyleKind.Positional)>] union Keep an adjacent positional contract such as FSharp.SystemTextJson's default. Schema.unionWith
[<DeriveUnion(Representation = UnionRepresentationKind.External, PayloadStyle = UnionPayloadStyleKind.NamedWithUnwrappedSingle, UnwrapFieldless = true)>] union Keep external tagging with named multi-field and unwrapped empty/single-field cases. Schema.unionWith
[<SchemaName "wire_name">] record field or nullary union case Override the configured wire name or case tag. fieldAs "wire_name" _.Field
[<SchemaConstructor>] static member Call this member with fields in declaration order instead of constructing a record literal. construct

Field attributes

Attribute Short description Schema DSL equivalent
[<Pattern "expr">] Require text to match a regular expression. constrain (pattern "expr")
[<Min n>] Set the minimum natural length of text, list, or map. constrain (minLength n)
[<Max n>] Set the maximum natural length of text, list, or map. constrain (maxLength n)
[<Length n>] Require an exact natural length. constrain (length n)
[<LengthBetween(min, max)>] Bound natural length inclusively. constrain (lengthBetween min max)
[<Present>] Require a string, collection, or optional value to be present/non-empty. constrain present
[<Supplied>] Require the input object to contain this field key. mustSupply
[<Format "name">] Attach open format metadata without adding a check. format (SchemaFormat.create "name")
[<AtLeast n>] Inclusive numeric lower bound (>=). constrain (atLeast n)
[<GreaterThan n>] Exclusive numeric lower bound (>). constrain (greaterThan n)
[<AtMost n>] Inclusive numeric upper bound (<=). constrain (atMost n)
[<LessThan n>] Exclusive numeric upper bound (<). constrain (lessThan n)
[<MultipleOf n>] Require a numeric value to be a whole multiple of n. constrain (multipleOf n)
[<Distinct>] Require all list elements to be distinct. constrain distinct
[<Email>] Apply the built-in email constraint to text. constrain email
[<Default value>] Supply a value when the input key is omitted; invalid on option fields. defaultValue value

Numeric bound and default attributes accept source literals supported by their target field. The generator reads the literal text, preserving decimal precision rather than round-tripping it through reflection.

Example

[<DeriveSchema>]
type Product =
    { [<SchemaName "product_code"; Pattern "^[A-Z0-9]+$"; Present>]
      Code: string
      [<AtLeast 0; LessThan 1000000>]
      Price: decimal
      [<Min 1; Distinct>]
      Tags: string list
      [<Default true>]
      Active: bool }

Multiple constraints are emitted in attribute order and execute like multiple constrain operations in a field block. Doc comments on records and fields become schema descriptions and generated XML documentation.