Skip to content

Commit 7d60609

Browse files
harrysolovayeffect-botgcanti
committed
Add Schema.isNonBlank check (#11248)
Co-authored-by: Effect Bot <tech-ops@effectful.co> Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
1 parent bd165e3 commit 7d60609

10 files changed

Lines changed: 96 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Add `Schema.isNonBlank` to reject empty and whitespace-only strings while preserving accepted strings unchanged, including surrounding whitespace. Add `SchemaRepresentation.isNonBlankReviver` to reconstruct persisted checks.

‎packages/effect/src/Schema.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6920,6 +6920,36 @@ export function isTrimmed(annotations?: Annotations.Filter) {
69206920
}
69216921
)
69226922
}
6923+
6924+
/**
6925+
* Checks for at least one non-whitespace character, preserving the string
6926+
* unchanged, including leading and trailing whitespace.
6927+
*
6928+
* **When to use**
6929+
*
6930+
* Use when free-form text is required. To also forbid surrounding whitespace,
6931+
* combine {@link isNonEmpty} with {@link isTrimmed} instead.
6932+
*
6933+
* **Details**
6934+
*
6935+
* Rejects empty and whitespace-only strings using the same whitespace semantics
6936+
* as `String.hasNonWhitespace`. Supports JSON Schema pattern output and arbitrary
6937+
* generation through {@link isPattern}.
6938+
*
6939+
* @stability unstable
6940+
* @category validation
6941+
* @since 4.3.0
6942+
*/
6943+
export function isNonBlank(annotations?: Annotations.Filter) {
6944+
return isPattern(Str.NON_WHITESPACE_REGEXP, {
6945+
expected: "a string with at least one non-whitespace character",
6946+
representation: {
6947+
id: "effect/schema/isNonBlank",
6948+
payload: null
6949+
},
6950+
...annotations
6951+
})
6952+
}
69236953
/**
69246954
* Validates that a string contains no unpaired UTF-16 surrogates.
69256955
*

‎packages/effect/src/SchemaRepresentation.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,19 @@ export const isTrimmedReviver: FilterReviver<null> = makeReviverFilter(
805805
({ annotations }) => Schema.isTrimmed(annotations)
806806
)
807807

808+
/**
809+
* Reconstructs persisted {@link Schema.isNonBlank} checks.
810+
*
811+
* @stability unstable
812+
* @category validation
813+
* @since 4.3.0
814+
*/
815+
export const isNonBlankReviver: FilterReviver<null> = makeReviverFilter(
816+
"effect/schema/isNonBlank",
817+
Schema.Null,
818+
({ annotations }) => Schema.isNonBlank(annotations)
819+
)
820+
808821
/**
809822
* Reviver for persisted `isWellFormed` checks.
810823
*

‎packages/effect/src/String.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ interface WellFormedNative {
479479
export const isWellFormed = (self: string): boolean =>
480480
(self as WellFormedNative).isWellFormed?.() ?? !loneSurrogate.test(self)
481481

482+
/** @internal */
483+
export const NON_WHITESPACE_REGEXP = /\S/u
484+
482485
/**
483486
* Checks whether a string contains at least one non-whitespace character.
484487
*
@@ -502,7 +505,7 @@ export const isWellFormed = (self: string): boolean =>
502505
* @category predicates
503506
* @since 4.2.0
504507
*/
505-
export const hasNonWhitespace = (self: string): boolean => /\S/u.test(self)
508+
export const hasNonWhitespace = (self: string): boolean => NON_WHITESPACE_REGEXP.test(self)
506509

507510
/**
508511
* Returns the JavaScript string length, measured in UTF-16 code units.

‎packages/effect/src/internal/schema/schemaCodegenBuiltIns.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function between(render: PrintArgument): PrintArgument {
5252

5353
const checkArguments: Record<string, PrintArgument> = {
5454
isTrimmed: noArguments,
55+
isNonBlank: noArguments,
5556
isWellFormed: noArguments,
5657
isPattern: (payload) => {
5758
if (!isObject(payload) || typeof payload.source !== "string" || typeof payload.flags !== "string") return undefined

‎packages/effect/test/schema/Schema.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10884,6 +10884,23 @@ describe("Check", () => {
1088410884
)
1088510885
})
1088610886

10887+
it("isNonBlank", async () => {
10888+
const schema = Schema.String.check(Schema.isNonBlank())
10889+
const asserts = new TestSchema.Asserts(schema)
10890+
10891+
if (verifyGeneration) {
10892+
asserts.arbitrary().verifyGeneration()
10893+
}
10894+
10895+
const decoding = asserts.decoding()
10896+
await decoding.succeed(" text ")
10897+
await decoding.succeed("hello\n")
10898+
await decoding.succeed("\u200b")
10899+
const message = "Expected a string with at least one non-whitespace character"
10900+
await decoding.fail("", message)
10901+
await decoding.fail(" \t\r\n\u00a0\u2028\ufeff", message)
10902+
})
10903+
1088710904
it("isAscii", async () => {
1088810905
const schema = Schema.String.check(Schema.isAscii())
1088910906
const asserts = new TestSchema.Asserts(schema)

‎packages/effect/test/schema/representation/builtInRevivers.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ describe("SchemaRepresentation built-in string revivers", () => {
208208
})
209209
})
210210

211+
it("revives isNonBlank", () => {
212+
assertFilterReviver({
213+
schema: Schema.String.check(Schema.isNonBlank()),
214+
id: "effect/schema/isNonBlank",
215+
payload: null,
216+
reviver: SchemaRepresentation.isNonBlankReviver,
217+
valid: " text ",
218+
invalid: " "
219+
})
220+
})
221+
211222
it("revives isTrimmed", () => {
212223
assertFilterReviver({
213224
schema: Schema.String.check(Schema.isTrimmed()),

‎packages/effect/test/schema/representation/codegen.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ describe("Codegen.Module.addSchemas", { concurrent: false }, () => {
316316
["includes", Schema.isIncluding("a"), ["bab"], ["bb"]],
317317
["GUID", Schema.isGUID({ message: "message" }), ["00000000-0000-0000-0000-000000000000"], ["a"]],
318318
["ASCII", Schema.isAscii(), ["abc"], ["é"]],
319+
["non blank", Schema.isNonBlank(), [" a "], ["", " "]],
319320
["pattern and flags", Schema.isPattern(/^a+$/i), ["AAA"], ["b"]]
320321
]
321322
for (const [name, check, accepts, rejects] of stringChecks) {

‎packages/effect/test/schema/toJsonSchemaDocument.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,19 @@ describe("toJsonSchemaDocument", () => {
13221322
})
13231323
})
13241324

1325+
it("isNonBlank", () => {
1326+
assertJsonSchemaDocument(
1327+
Schema.String.annotate({ description: "description" }).check(Schema.isNonBlank()),
1328+
{
1329+
schema: {
1330+
"type": "string",
1331+
"description": "description",
1332+
"pattern": "\\S"
1333+
}
1334+
}
1335+
)
1336+
})
1337+
13251338
it("isLowercased", () => {
13261339
const schema = Schema.String.check(Schema.isLowercased())
13271340
assertJsonSchemaDocument(schema, {

‎packages/effect/typetest/schema/SchemaBuiltInRevivers.tst.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ describe("Schema built-in revivers", () => {
55
it("composes every built-in reviver without casts", () => {
66
const revivers: ReadonlyArray<SchemaRepresentation.AnyReviver> = [
77
SchemaRepresentation.isTrimmedReviver,
8+
SchemaRepresentation.isNonBlankReviver,
89
SchemaRepresentation.isPatternReviver,
910
SchemaRepresentation.isStringFiniteReviver,
1011
SchemaRepresentation.isStringBigIntReviver,

0 commit comments

Comments
 (0)