Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Add ulid schema #318

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-masks-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": minor
---

Add schema for [ULID](https://github.com/ulid/spec)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ S.never;

S.json;
S.UUID;
S.ULID;
```

## Literals
Expand Down
26 changes: 24 additions & 2 deletions docs/modules/Schema.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Added in v1.0.0
- [constructors](#constructors)
- [JsonNumber](#jsonnumber)
- [UUID](#uuid)
- [ULID](#ulid)
- [chunkFromSelf](#chunkfromself)
- [enums](#enums)
- [instanceOf](#instanceof)
Expand Down Expand Up @@ -129,7 +130,7 @@ Added in v1.0.0
- [nonNaN](#nonnan)
- [nonNegative](#nonnegative)
- [nonPositive](#nonpositive)
- [numberFromString](#numberfromstring)
- [numberFromString](#numberfromstring-1)
- [positive](#positive)
- [option](#option-1)
- [optionFromNullable](#optionfromnullable)
Expand Down Expand Up @@ -163,7 +164,7 @@ Added in v1.0.0
- [nonEmpty](#nonempty)
- [pattern](#pattern)
- [startsWith](#startswith)
- [trim](#trim)
- [trim](#trim-1)
- [trimmed](#trimmed)
- [type id](#type-id)
- [BetweenBigintTypeId](#betweenbiginttypeid)
Expand Down Expand Up @@ -202,6 +203,7 @@ Added in v1.0.0
- [StartsWithTypeId](#startswithtypeid)
- [TrimmedTypeId](#trimmedtypeid)
- [UUIDTypeId](#uuidtypeid)
- [ULIDTypeId](#ulidtypeid)
- [ValidDateTypeId](#validdatetypeid)
- [utils](#utils)
- [FromOptionalKeys (type alias)](#fromoptionalkeys-type-alias)
Expand Down Expand Up @@ -1078,6 +1080,16 @@ export declare const UUID: Schema<string, string>

Added in v1.0.0

## ULID

**Signature**

```ts
export declare const ULID: Schema<string, string>
```

Added in v1.0.0

## chunkFromSelf

**Signature**
Expand Down Expand Up @@ -2367,6 +2379,16 @@ export declare const UUIDTypeId: '@effect/schema/UUIDTypeId'

Added in v1.0.0

## ULIDTypeId

**Signature**

```ts
export declare const ULIDTypeId: '@effect/schema/ULIDTypeId'
```

Added in v1.0.0

## ValidDateTypeId

**Signature**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@
"dependencies": {
"@effect/data": "^0.12.10",
"@effect/io": "^0.29.0",
"fast-check": "^3.10.0"
"fast-check": "^3.11.0"
}
}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2751,6 +2751,27 @@ export const UUID: Schema<string> = pipe(
})
)

/**
* @category type id
* @since 1.0.0
*/
export const ULIDTypeId = "@effect/schema/ULIDTypeId"

const ulidRegex = /^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i

/**
* @category constructors
* @since 1.0.0
*/
export const ULID: Schema<string> = pipe(
string,
pattern(ulidRegex, {
typeId: ULIDTypeId,
title: "ULID",
arbitrary: (): Arbitrary<string> => (fc) => fc.ulid()
})
)

/**
* @category string
* @since 1.0.0
Expand Down
1 change: 1 addition & 0 deletions test/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe.concurrent("Schema", () => {
expect(S.EndsWithTypeId).exist
expect(S.IncludesTypeId).exist
expect(S.UUIDTypeId).exist
expect(S.ULIDTypeId).exist

expect(S.nullable).exist

Expand Down
18 changes: 18 additions & 0 deletions test/data/filter/ULID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"

describe.concurrent("ULID", () => {
it("property tests", () => {
Util.roundtrip(S.ULID)
})

it("Decoder", async () => {
const schema = S.ULID
await Util.expectParseSuccess(schema, "01H4PGGGJVN2DKP2K1H7EH996V")
await Util.expectParseFailure(
schema,
"",
`Expected ULID, actual ""`
)
})
})
Loading