Skip to content

Commit ab5198c

Browse files
committed
rename isPropertiesLength to isPropertiesLengthBetween
1 parent 0c31b6b commit ab5198c

File tree

6 files changed

+42
-37
lines changed

6 files changed

+42
-37
lines changed

packages/effect/src/Schema.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5111,39 +5111,42 @@ export function isMaxProperties(maxProperties: number, annotations?: Annotations
51115111
}
51125112

51135113
/**
5114-
* Validates that an object contains exactly the specified number of properties.
5114+
* Validates that an object contains between `minimum` and `maximum` properties (inclusive).
51155115
* This includes both string and symbol keys when counting properties.
51165116
*
51175117
* **JSON Schema**
51185118
*
5119-
* This check corresponds to both `minProperties` and `maxProperties`
5120-
* constraints in JSON Schema, both set to the same value.
5119+
* This check corresponds to `minProperties` and `maxProperties`
5120+
* constraints in JSON Schema.
51215121
*
51225122
* **Arbitrary**
51235123
*
5124-
* When generating test data with fast-check, this applies both `minLength` and
5124+
* When generating test data with fast-check, this applies `minLength` and
51255125
* `maxLength` constraints to the array of entries that is generated before
5126-
* being converted to an object, ensuring the resulting object has exactly the
5127-
* required number of properties.
5126+
* being converted to an object.
51285127
*
51295128
* @category Object checks
51305129
* @since 4.0.0
51315130
*/
5132-
export function isPropertiesLength(length: number, annotations?: Annotations.Filter) {
5133-
length = Math.max(0, Math.floor(length))
5131+
export function isPropertiesLengthBetween(minimum: number, maximum: number, annotations?: Annotations.Filter) {
5132+
minimum = Math.max(0, Math.floor(minimum))
5133+
maximum = Math.max(0, Math.floor(maximum))
51345134
return makeFilter<object>(
5135-
(input) => Reflect.ownKeys(input).length === length,
5135+
(input) => Reflect.ownKeys(input).length >= minimum && Reflect.ownKeys(input).length <= maximum,
51365136
{
5137-
expected: `a value with exactly ${length === 1 ? "1 entry" : `${length} entries`}`,
5137+
expected: minimum === maximum
5138+
? `a value with exactly ${minimum === 1 ? "1 entry" : `${minimum} entries`}`
5139+
: `a value with between ${minimum} and ${maximum} entries`,
51385140
meta: {
5139-
_tag: "isPropertiesLength",
5140-
length
5141+
_tag: "isPropertiesLengthBetween",
5142+
minimum,
5143+
maximum
51415144
},
51425145
[AST.STRUCTURAL_ANNOTATION_KEY]: true,
51435146
toArbitraryConstraint: {
51445147
array: {
5145-
minLength: length,
5146-
maxLength: length
5148+
minLength: minimum,
5149+
maxLength: maximum
51475150
}
51485151
},
51495152
...annotations
@@ -9018,9 +9021,10 @@ export declare namespace Annotations {
90189021
readonly _tag: "isMaxProperties"
90199022
readonly maxProperties: number
90209023
}
9021-
readonly isPropertiesLength: {
9022-
readonly _tag: "isPropertiesLength"
9023-
readonly length: number
9024+
readonly isPropertiesLengthBetween: {
9025+
readonly _tag: "isPropertiesLengthBetween"
9026+
readonly minimum: number
9027+
readonly maximum: number
90249028
}
90259029
readonly isPropertyNames: {
90269030
readonly _tag: "isPropertyNames"

packages/effect/src/SchemaRepresentation.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ export type ObjectsMeta =
657657
| Schema.Annotations.BuiltInMetaDefinitions[
658658
| "isMinProperties"
659659
| "isMaxProperties"
660-
| "isPropertiesLength"
660+
| "isPropertiesLengthBetween"
661661
]
662662
| { readonly _tag: "isPropertyNames"; readonly propertyNames: Representation }
663663

@@ -1341,10 +1341,11 @@ const $IsMaxProperties = Schema.Struct({
13411341
maxProperties: NonNegativeInt
13421342
}).annotate({ identifier: "IsMaxProperties" })
13431343

1344-
const $IsPropertiesLength = Schema.Struct({
1345-
_tag: Schema.tag("isPropertiesLength"),
1346-
length: NonNegativeInt
1347-
}).annotate({ identifier: "IsPropertiesLength" })
1344+
const $IsPropertiesLengthBetween = Schema.Struct({
1345+
_tag: Schema.tag("isPropertiesLengthBetween"),
1346+
minimum: NonNegativeInt,
1347+
maximum: NonNegativeInt
1348+
}).annotate({ identifier: "IsPropertiesLengthBetween" })
13481349

13491350
const $IsPropertyNames = Schema.Struct({
13501351
_tag: Schema.tag("isPropertyNames"),
@@ -1360,7 +1361,7 @@ const $IsPropertyNames = Schema.Struct({
13601361
export const $ObjectsMeta = Schema.Union([
13611362
$IsMinProperties,
13621363
$IsMaxProperties,
1363-
$IsPropertiesLength,
1364+
$IsPropertiesLengthBetween,
13641365
$IsPropertyNames
13651366
]).annotate({ identifier: "ObjectsMeta" })
13661367

@@ -2059,8 +2060,8 @@ export function toSchema<S extends Schema.Top = Schema.Top>(document: Document,
20592060
return Schema.isMinProperties(filter.meta.minProperties, a)
20602061
case "isMaxProperties":
20612062
return Schema.isMaxProperties(filter.meta.maxProperties, a)
2062-
case "isPropertiesLength":
2063-
return Schema.isPropertiesLength(filter.meta.length, a)
2063+
case "isPropertiesLengthBetween":
2064+
return Schema.isPropertiesLengthBetween(filter.meta.minimum, filter.meta.maximum, a)
20642065
case "isPropertyNames":
20652066
return Schema.isPropertyNames(recur(filter.meta.propertyNames) as Schema.Record.Key, a)
20662067

@@ -2686,8 +2687,8 @@ export function toCodeDocument(multiDocument: MultiDocument, options?: {
26862687
return `Schema.isMinProperties(${filter.meta.minProperties}${ca})`
26872688
case "isMaxProperties":
26882689
return `Schema.isMaxProperties(${filter.meta.maxProperties}${ca})`
2689-
case "isPropertiesLength":
2690-
return `Schema.isPropertiesLength(${filter.meta.length}${ca})`
2690+
case "isPropertiesLengthBetween":
2691+
return `Schema.isPropertiesLengthBetween(${filter.meta.minimum}, ${filter.meta.maximum}${ca})`
26912692
case "isPropertyNames":
26922693
return `Schema.isPropertyNames(${recur(filter.meta.propertyNames).runtime}${ca})`
26932694

packages/effect/src/internal/schema/representation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,8 @@ export function toJsonSchemaMultiDocument(
697697
return { minProperties: meta.minProperties }
698698
case "isMaxProperties":
699699
return { maxProperties: meta.maxProperties }
700-
case "isPropertiesLength":
701-
return { minProperties: meta.length, maxProperties: meta.length }
700+
case "isPropertiesLengthBetween":
701+
return { minProperties: meta.minimum, maxProperties: meta.maximum }
702702
case "isPropertyNames":
703703
return { propertyNames: recur(meta.propertyNames) }
704704

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,8 +1621,8 @@ Expected a value between -2147483648 and 2147483647, got 9007199254740992`
16211621
)
16221622
})
16231623

1624-
it("isPropertiesLength", async () => {
1625-
const schema = Schema.Record(Schema.String, Schema.Number).check(Schema.isPropertiesLength(2))
1624+
it("isPropertiesLengthBetween", async () => {
1625+
const schema = Schema.Record(Schema.String, Schema.Number).check(Schema.isPropertiesLengthBetween(2, 2))
16261626
const asserts = new TestSchema.Asserts(schema)
16271627

16281628
const decoding = asserts.decoding()
@@ -1638,11 +1638,11 @@ Expected a value between -2147483648 and 2147483647, got 9007199254740992`
16381638
)
16391639
})
16401640

1641-
it("isPropertiesLength with symbol keys", async () => {
1641+
it("isPropertiesLengthBetween with symbol keys", async () => {
16421642
const sym1 = Symbol("test1")
16431643
const sym2 = Symbol("test2")
16441644
const schema = Schema.Record(Schema.Union([Schema.String, Schema.Symbol]), Schema.Number).check(
1645-
Schema.isPropertiesLength(2)
1645+
Schema.isPropertiesLengthBetween(2, 2)
16461646
)
16471647
const asserts = new TestSchema.Asserts(schema)
16481648

packages/effect/test/schema/toArbitrary.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,9 @@ describe("Arbitrary generation", () => {
471471
)
472472
})
473473

474-
it("isPropertiesLength(2)", () => {
474+
it("isPropertiesLengthBetween(2, 2)", () => {
475475
verifyGeneration(
476-
Schema.Record(Schema.String, Schema.Number).check(Schema.isPropertiesLength(2))
476+
Schema.Record(Schema.String, Schema.Number).check(Schema.isPropertiesLengthBetween(2, 2))
477477
)
478478
})
479479

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,9 +2687,9 @@ describe("toJsonSchemaDocument", () => {
26872687
)
26882688
})
26892689

2690-
it("isPropertiesLength", () => {
2690+
it("isPropertiesLengthBetween", () => {
26912691
assertJsonSchemaDocument(
2692-
Schema.Record(Schema.String, Schema.Finite).check(Schema.isPropertiesLength(2)),
2692+
Schema.Record(Schema.String, Schema.Finite).check(Schema.isPropertiesLengthBetween(2, 2)),
26932693
{
26942694
schema: {
26952695
"type": "object",

0 commit comments

Comments
 (0)