Skip to content

Schema: align built-in check mappings with JSON Schema semantics #8358

Closed

Description

@gcanti

Problem

On main, some built-in Schema checks and their JSON Schema translations accept different values, even when both
validate the same untransformed JSON input. The reverse translation can also reconstruct a check with
different semantics from the source keyword.

This issue focuses on those individual check mappings. It does not propose a general exact-conversion
API or changes to decoding, property stripping, codecs or structural schema conversion.

Related: #8178.

Minimal reproduction

import { JsonSchema, Schema, SchemaRepresentation } from "effect"

// Export: the Effect check counts UTF-16 code units.
const original = Schema.String.check(Schema.isMinLength(2))

console.log(Schema.is(original)("😀")) // true
console.log(Schema.toJsonSchemaDocument(original).schema)
// { type: "string", minLength: 2 }
// JSON Schema rejects "😀", which contains one code point.

// Import: the JSON Schema allows one code point.
const imported = SchemaRepresentation.fromJsonSchemaDocument(
  JsonSchema.fromSchemaDraft2020_12({ type: "string", maxLength: 1 })
)

console.log(Schema.is(imported)("😀")) // false
// The source JSON Schema accepts this value.

String lengths and integer validation are defined by the
JSON Schema validation vocabulary.

Mappings to address

Checks / keywords Current difference Required work
isMinLength, isMaxLength, isLengthBetween ↔ minLength, maxLength Effect counts UTF-16 code units; JSON Schema counts code points. Import string bounds with code-point checks. Preserve the existing UTF-16 API semantics and decide how export represents them without substituting code-point bounds. Keep the array mapping to minItems / maxItems.
isPattern ↔ pattern Export drops flags; import with patterns: "apply" constructs a pattern without u. Define supported regex semantics, preserve equivalent behavior where possible and report unsupported mappings otherwise.
isUppercased, isLowercased, isCapitalized, isUncapitalized → pattern JavaScript casing is Unicode-aware, but the exported patterns only exclude ASCII letters. Replace the approximations with equivalent constraints or explicitly report the unsupported mapping.
isStartsWith, isEndsWith, isIncludes → pattern UTF-16 substring operations can split surrogate pairs; Unicode regex matching behaves differently. Check literal arguments for this mismatch and translate or report unsupported cases.
isInt ↔ type: "integer" isInt requires safe integers; JSON Schema does not impose that range. Add safe-integer bounds on export. Import integer with a predicate that does not impose the safe range. Preserve the existing meaning of isInt.
Generic length/property checks → type-specific keywords minLength does not constrain an object's length field; maxProperties does not constrain an array. Check that the keyword applies to the check's input domain. Translate the actual predicate or report the unsupported mapping.
Cardinality parameters → numeric keyword values Some constructors retain NaN or Infinity, which serialize to null. Reject invalid parameters or explicitly handle tautological/impossible cases. Emit valid JSON Schema keyword values.

String lengths

On main, the importer uses the UTF-16 checks for minLength and maxLength.
Add code-point checks for these keywords, as proposed in #8178, while retaining the existing .length semantics.

  • Add code-point checks, use them for imported string-length keywords and register the required importer-owned
    revivers. Update importer-side evaluation of these checks as well.
  • Keep minItems and maxItems mapped to array-length checks.
  • Preserve the behavior of the existing UTF-16 checks. Use an equivalent export where supported, or report
    the unsupported mapping instead of treating code-point bounds as equivalent.
  • Retain cases where equivalence is established, such as isNonEmpty on strings and arrays.
  • Handle non-finite bounds in the affected cardinality constructors or their translation.

Tests should cover ASCII, BMP and astral characters, empty strings, combining sequences and lone surrogates
under the chosen Unicode policy. "😀" must fail minLength: 2 and pass maxLength: 1.
"e\u0301" contains two code points, regardless of its appearance as one grapheme.

Regular expressions

JSON Schema stores a pattern source without a separate JavaScript flags field. Dropping flags or blindly
adding u does not preserve every RegExp.

  • Define the supported ECMAScript/Unicode behavior for imported and exported patterns.
  • Under a Unicode policy, compile applied imports with u and report invalid syntax with its source path.
  • Cover i, m, s, y and differences between non-Unicode, u and v modes on export.
  • Treat g and d separately. isPattern resets lastIndex before each test, and match indices do not
    affect acceptance. These flags alone do not establish a mismatch.
  • Preserve the existing explicit refusal with patterns: "error"; patterns: "ignore" intentionally
    discards the constraint and is not an equivalent mapping.

The specification recommends Unicode support with a SHOULD, so the chosen behavior must be documented.
Patterns perform searches unless explicitly anchored.
JSON Schema Core, regular expressions.

Casing, substring checks and integers

  • Test casing beyond ASCII, including expansions and contextual behavior. A Unicode property escape
    is not automatically equivalent to the JavaScript casing predicate.
  • Preserve the charAt(0) semantics of isCapitalized and isUncapitalized.
  • Cover literal prefix/suffix/substring arguments containing lone surrogate code units, plus empty strings
    and escaped regex metacharacters.
  • Export isInt as integer with safe lower and upper bounds, conjoined with any existing bounds.
  • Import JSON Schema integers with an appropriate finite integer predicate, including representable integers
    outside the safe range. isInt32 and isUint32 already have bounds that avoid this mismatch.

Regression cases

The following export results were reproduced with Ajv 8.20.0, Draft 2020-12, unicodeRegExp: true and
validateFormats: false. String checks below are applied to Schema.String.

Check Input Effect Exported JSON Schema
isMinLength(2) "😀" Accepts Rejects
isMaxLength(1) "😀" Rejects Accepts
isPattern(/^abc$/i) "ABC" Accepts Rejects
isPattern(/a/y) "ba" Rejects Accepts
isPattern(/^.$/) "😀" Rejects Accepts
isPattern(/\a/) "a" Accepts Fails Unicode regex compilation
isUppercased() "é" Rejects Accepts
isLowercased() "É" Rejects Accepts
isCapitalized() "éclair" Rejects Accepts
isUncapitalized() "Éclair" Rejects Accepts
isStartsWith("\uD83D") "😀" Accepts Rejects
isEndsWith("\uDE00") "😀" Accepts Rejects
isIncludes("\uD83D") "😀" Accepts Rejects
isInt() on Schema.Number 9007199254740992 Rejects Accepts

Also test both directions independently:

  • Import minLength: 2 and maxLength: 1 against "😀".
  • Import type: "integer" against 9007199254740992 and 1e20.
  • Apply imported pattern: "^.$" to "😀" under the documented Unicode behavior.
  • Check isMinLength(2) on { length: 1 } and isMaxProperties(1) on ["a"].
  • Serialize documents produced from non-finite cardinality bounds before checking their validity.

Activity

  1. lloydrichards commented on Sep 22, 2026

    @lloydrichards
    Contributor

    maybe this is of interest to you cause I was dealing with the same issue when rendering consistent sizes to the terminal but I ended up needing to separate the length of a string from the width:

    https://github.com/lloydrichards/effect-boxes/blob/main/packages/effect-boxes/src/internal/width.ts

    There are so many gotcha's especially once you get into East Asian characters that it might be worth having a look as well at the unicode tests I've been using.

    I've been wanting to wait til v4 is release to see if porting boxes would make sense, but certainly a String.width functionality might benefit you here before then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions