Hyperlinkv0.8.0-beta.28

Schema

Schema.isMinLengthfunctioneffect/Schema.ts:7986
(minLength: number, annotations?: Annotations.Filter): SchemaAST.Filter<{
  readonly length: number
}>

Validates that a value has at least the specified length. Works with strings and arrays.

Details

JSON Schema:

This check corresponds to the minLength constraint for strings or the minItems constraint for arrays in JSON Schema.

Arbitrary:

When generating test data with fast-check, this applies a minLength constraint to ensure generated strings or arrays have at least the required length.

Example (Checking minimum length)

import { Schema } from "effect"

const NonEmptyStringSchema = Schema.String.check(Schema.isMinLength(1))
const NonEmptyArraySchema = Schema.Array(Schema.Number).check(Schema.isMinLength(1))
Length checks
Source effect/Schema.ts:798620 lines
export function isMinLength(minLength: number, annotations?: Annotations.Filter) {
  minLength = Math.max(0, Math.floor(minLength))
  return makeFilter<{ readonly length: number }>(
    (input) => input.length >= minLength,
    {
      expected: `a value with a length of at least ${minLength}`,
      meta: {
        _tag: "isMinLength",
        minLength
      },
      [SchemaAST.STRUCTURAL_ANNOTATION_KEY]: true,
      arbitrary: {
        constraint: {
          minLength
        }
      },
      ...annotations
    }
  )
}
Referenced by 2 symbols