Skip to content

Commit

Permalink
add ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed Nov 26, 2024
1 parent 5dd8b6f commit 44e551f
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 9 deletions.
133 changes: 126 additions & 7 deletions ark/docs/src/content/docs/objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,69 @@ const arrays = type({
<a name="arrays/lengths" />
##### lengths

🚧 Coming soon ™️🚧
Constrain an array with an inclusive or exclusive min or max length.

<Tabs>
<SyntaxTab string>

```ts
const bounded = type({
nonEmptyStringArray: "string[] > 0"
atLeast3Integers: "number.integer[] >= 3",
lessThan10Emails: "string.email[] < 10",
atMost5Booleans: "boolean[] <= 5"
})
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const bounded = type({
nonEmptyStringArray: type.string.array().moreThanLength(0),
atLeast3Integers: type.keywords.number.integer.array().atLeastLength(3),
lessThan10Emails: type.keywords.string.email.array().lessThanLength(10),
atMost5Booleans: type.boolean.array().atMostLength(5)
})
```

</SyntaxTab>

</Tabs>

Range expressions allow you to specify both a min and max length and use the same syntax for exclusivity.

<Tabs>
<SyntaxTab string>

```ts
const range = type({
nonEmptyStringArrayAtMostLength10: "0 < string[] <= 10",
twoToFiveIntegers: "2 <= number.integer[] < 6"
})
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const range = type({
nonEmptyStringArrayAtMostLength10: type.string
.array()
.moreThanLength(0)
.atMostLength(10),
twoToFiveIntegers: type.keywords.number.integer
.array()
.atLeastLength(2)
.lessThanLength(6)
})
```

</SyntaxTab>

</Tabs>

## tuples

Expand Down Expand Up @@ -447,11 +509,6 @@ const myTuple = type(["...", type.number.array(), type.boolean, type.string])

## dates

<a name="dates/keywords" />
##### keywords

🚧 Coming soon ™️🚧

<a name="dates/literals" />
##### literals

Expand All @@ -469,7 +526,69 @@ const literals = type({
<a name="dates/ranges" />
##### ranges

🚧 Coming soon ™️🚧
Constrain a Date with an inclusive or exclusive min or max.

Bounds can be expressed as either a [number](/primitives#number/literals) representing its corresponding Unix epoch value or a [Date literal](/objects#dates/literals).

<Tabs>
<SyntaxTab string>

```ts
const bounded = type({
dateInThePast: `Date < ${Date.now()}`,
dateAfter2000: "Date > d'2000-01-01'",
dateAtOrAfter1970: "Date >= 0"
})
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const bounded = type({
dateInThePast: type.Date.earlierThan(Date.now()),
dateAfter2000: type.Date.laterThan("2000-01-01"),
dateAtOrAfter1970: type.Date.atOrAfter(0)
})
```

</SyntaxTab>

</Tabs>

Range expressions allow you to specify both a min and max and use the same syntax for exclusivity.

<Tabs>
<SyntaxTab string>

```ts
const tenYearsAgo = new Date()
.setFullYear(new Date().getFullYear() - 10)
.valueOf()

const bounded = type({
dateInTheLast10Years: `${tenYearsAgo} <= Date < ${Date.now()}`
})
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const tenYearsAgo = new Date()
.setFullYear(new Date().getFullYear() - 10)
.valueOf()

const bounded = type({
dateInTheLast10Years: type.Date.atOrAfter(tenYearsAgo).earlierThan(Date.now())
})
```

</SyntaxTab>

</Tabs>

## instanceof

Expand Down
121 changes: 119 additions & 2 deletions ark/docs/src/content/docs/primitives.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,65 @@ const literals = type({
<a name="string/lengths" />
##### lengths

🚧 Coming soon ™️🚧
Constrain a string with an inclusive or exclusive min or max length.

<Tabs>
<SyntaxTab string>

```ts
const bounded = type({
nonEmpty: "string > 0"
atLeastLength3: "string.alphanumeric >= 3",
lessThanLength10: "string < 10",
atMostLength5: "string <= 5"
})
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const bounded = type({
nonEmpty: type.string.moreThanLength(0)
atLeastLength3: type.keywords.string.alphanumeric.atLeastLength(3),
lessThanLength10: type.string.lessThanLength(10),
atMostLength5: type.string.atMostLength(5)
})
```

</SyntaxTab>

</Tabs>

Range expressions allow you to specify both a min and max length and use the same syntax for exclusivity.

<Tabs>
<SyntaxTab string>

```ts
const range = type({
nonEmptyAtMostLength10: "0 < string <= 10",
integerStringWith2To5Digits: "2 <= string.integer < 6"
})
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const range = type({
nonEmptyAtMostLength10: type.string.moreThanLength(0).atMostLength(10),
integerStringWith2To5Digits: type.keywords.string.integer
.atLeastLength(2)
.lessThanLength(6)
})
```

</SyntaxTab>

</Tabs>

## number

Expand All @@ -60,7 +118,66 @@ const literals = type({
<a name="number/ranges" />
##### ranges

🚧 Coming soon ™️🚧
Constrain a number with an inclusive or exclusive min or max.

<Tabs>
<SyntaxTab string>

```ts
const bounded = type({
positive: "number > 0"
atLeast3: "number.integer >= 3",
lessThanPi: "number < 3.14159",
atMost5: "number <= 5"
})
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const bounded = type({
positive: type.number.moreThan(0)
atLeast3: type.keywords.number.integer.atLeast(3),
lessThanPi: type.number.lessThan(3.14159),
atMost5: type.number.atMost(5)
})
```

</SyntaxTab>

</Tabs>

Range expressions allow you to specify both a min and max and use the same syntax for exclusivity.

<Tabs>
<SyntaxTab string>

```ts
const range = type({
positiveAtMostE: "0 < number <= 2.71828",
evenNumberAbsoluteValueLessThan50: "-50 < (number % 2) < 50"
})
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const range = type({
positiveAtMostE: type.number.moreThan(0).atMost(2.71828),
evenNumberAbsoluteValueLessThan50: type.number
.divisibleBy(2)
.moreThan(-50)
.lessThan(50)
})
```

</SyntaxTab>

</Tabs>

<a name="number/divisors" />
##### divisors
Expand Down
22 changes: 22 additions & 0 deletions ark/repo/scratch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,25 @@ interface User extends type.infer<typeof _user> {}
const user: type<User> = _user

user({}).toString()

Date.now()

const tenYearsAgo = new Date()
.setFullYear(new Date().getFullYear() - 10)
.valueOf()

const bounded = type({
dateInTheLast10Years: `${tenYearsAgo} <= Date < ${Date.now()}`
})

Date.now() //?

//?

type.Date.laterThan("")

const out = bounded.assert({
dateInThePast: new Date(0),
dateAfter2000: new Date(),
dateAtOrAfter1970: new Date(0)
})

0 comments on commit 44e551f

Please sign in to comment.