(
minimum: number,
maximum: number,
annotations?: Annotations.Filter
): SchemaAST.Filter<{ readonly length: number }>Validates that a value's length is within the specified range. Works with strings and arrays.
Details
JSON Schema:
This check corresponds to minLength/maxLength constraints for strings
or minItems/maxItems constraints for arrays in JSON Schema.
Arbitrary:
When generating test data with fast-check, this applies minLength and
maxLength constraints to ensure generated strings or arrays have a length
within the specified range.
export function function isLengthBetween(
minimum: number,
maximum: number,
annotations?: Annotations.Filter
): SchemaAST.Filter<{
readonly length: number
}>
Validates that a value's length is within the specified range. Works with
strings and arrays.
Details
JSON Schema:
This check corresponds to minLength/maxLength constraints for strings
or minItems/maxItems constraints for arrays in JSON Schema.
Arbitrary:
When generating test data with fast-check, this applies minLength and
maxLength constraints to ensure generated strings or arrays have a length
within the specified range.
isLengthBetween(minimum: numberminimum: number, maximum: numbermaximum: number, annotations: Annotations.Filter | undefinedannotations?: Annotations.interface Annotations.FilterAnnotations for filter schema nodes (created via Schema.filter). Extends
Augment
with an optional error message, identifier, and metadata.
Filters are intentionally non-parametric to keep them covariant.
Filter) {
minimum: numberminimum = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.max(...values: number[]): numberReturns the larger of a set of supplied numeric expressions.
max(0, var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.floor(x: number): numberReturns the greatest integer less than or equal to its numeric argument.
floor(minimum: numberminimum))
maximum: numbermaximum = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.max(...values: number[]): numberReturns the larger of a set of supplied numeric expressions.
max(0, var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.floor(x: number): numberReturns the greatest integer less than or equal to its numeric argument.
floor(maximum: numbermaximum))
return const makeFilter: <T>(
filter: (
input: T,
ast: SchemaAST.AST,
options: SchemaAST.ParseOptions
) => FilterOutput,
annotations?: Annotations.Filter | undefined,
abort?: boolean
) => SchemaAST.Filter<T>
Creates a custom validation filter from a predicate function.
Details
The predicate receives the decoded input value, the schema AST, and parse
options, and returns a FilterOutput. Non-success outputs are normalized into
schema issues. The annotations parameter annotates the filter itself; with
the default formatter, failures use message first, expected second, and
<filter> when neither is provided.
When abort is true, parsing stops after this filter fails instead of
collecting later check failures.
Example (Reporting failure at a nested path)
import { Schema } from "effect"
const schema = Schema.Struct({ password: Schema.String, confirmPassword: Schema.String }).check(
Schema.makeFilter((o) =>
o.password === o.confirmPassword
? undefined
: { path: ["password"], issue: "password and confirmPassword must match" }
)
)
console.log(String(Schema.decodeUnknownExit(schema)({ password: "123456", confirmPassword: "1234567" })))
// Failure(Cause([Fail(SchemaError: password and confirmPassword must match
// at ["password"])]))
Example (Reporting multiple failures at once)
import { Schema } from "effect"
const schema = Schema.Struct({ a: Schema.Finite, b: Schema.Finite, c: Schema.Finite }).check(
Schema.makeFilter((o) => {
const issues: Array<Schema.FilterIssue> = []
if (o.a > 0) {
if (o.b <= 0) issues.push({ path: ["b"], issue: "b must be greater than 0" })
if (o.c <= 0) issues.push({ path: ["c"], issue: "c must be greater than 0" })
}
return issues
})
)
console.log(String(Schema.decodeUnknownExit(schema)({ a: 1, b: 0, c: 0 })))
// Failure(Cause([Fail(SchemaError: b must be greater than 0
// at ["b"]
// c must be greater than 0
// at ["c"])]))
makeFilter<{ readonly length: numberlength: number }>(
(input: {
readonly length: number
}
input) => input: {
readonly length: number
}
input.length: numberlength >= minimum: numberminimum && input: {
readonly length: number
}
input.length: numberlength <= maximum: numbermaximum,
{
Annotations.Augment.expected?: string | undefinedHuman-readable description of what a value is expected to satisfy.
Details
For filter and refinement failures, the default formatter uses
message first, then expected, and finally falls back to <filter>.
Use this to name a failed filter in the default message:
Expected <expected>, got <actual>.
expected: minimum: numberminimum === maximum: numbermaximum
? `a value with a length of ${minimum: numberminimum}`
: `a value with a length between ${minimum: numberminimum} and ${maximum: numbermaximum}`,
Annotations.Filter.meta?: Annotations.Meta | undefined(property) Annotations.Filter.meta?: {
_tag: 'isLengthBetween';
minimum: number;
maximum: number;
}
Optional metadata used to identify or extend the filter with custom data.
meta: {
_tag: "isLengthBetween"_tag: "isLengthBetween",
minimum: numberminimum,
maximum: numbermaximum
},
[import SchemaASTSchemaAST.const STRUCTURAL_ANNOTATION_KEY: "~structural"STRUCTURAL_ANNOTATION_KEY]: true,
Annotations.Filter.arbitrary?: Annotations.ToArbitrary.Filter | undefined(property) Annotations.Filter.arbitrary?: {
constraint: { minLength: number; maxLength: number };
}
Optional hints used by arbitrary derivation for this filter.
Details
The same annotation can be attached to a single filter or a
FilterGroup. Group hints apply to the same schema node while child
filters are still collected and checked normally.
arbitrary: {
Annotations.ToArbitrary.Filter.constraint?: Annotations.ToArbitrary.GenerationConstraint | undefined(property) Annotations.ToArbitrary.Filter.constraint?: {
minLength: number;
maxLength: number;
}
constraint: {
Annotations.ToArbitrary.GenerationConstraint.minLength?: number | undefinedminLength: minimum: numberminimum,
Annotations.ToArbitrary.GenerationConstraint.maxLength?: number | undefinedmaxLength: maximum: numbermaximum
}
},
...annotations: Annotations.Filter | undefinedannotations
}
)
}