diff --git a/.changeset/cold-masks-sit.md b/.changeset/cold-masks-sit.md new file mode 100644 index 000000000..f8eb5a1a6 --- /dev/null +++ b/.changeset/cold-masks-sit.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": minor +--- + +Add schema for [ULID](https://github.com/ulid/spec) diff --git a/README.md b/README.md index 227b34ccc..884c792fb 100644 --- a/README.md +++ b/README.md @@ -433,6 +433,7 @@ S.never; S.json; S.UUID; +S.ULID; ``` ## Literals diff --git a/docs/modules/Schema.ts.md b/docs/modules/Schema.ts.md index 5072cf16a..4c9bcf00c 100644 --- a/docs/modules/Schema.ts.md +++ b/docs/modules/Schema.ts.md @@ -80,6 +80,7 @@ Added in v1.0.0 - [constructors](#constructors) - [JsonNumber](#jsonnumber) - [UUID](#uuid) + - [ULID](#ulid) - [chunkFromSelf](#chunkfromself) - [enums](#enums) - [instanceOf](#instanceof) @@ -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) @@ -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) @@ -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) @@ -1078,6 +1080,16 @@ export declare const UUID: Schema Added in v1.0.0 +## ULID + +**Signature** + +```ts +export declare const ULID: Schema +``` + +Added in v1.0.0 + ## chunkFromSelf **Signature** @@ -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** diff --git a/package.json b/package.json index b9c6b63f0..176cf1ce4 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index be572ae96..0f2ed2009 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,8 +12,8 @@ dependencies: specifier: ^0.29.0 version: 0.29.0 fast-check: - specifier: ^3.10.0 - version: 3.10.0 + specifier: ^3.11.0 + version: 3.11.0 devDependencies: '@babel/cli': @@ -3569,8 +3569,8 @@ packages: engines: {'0': node >=0.6.0} dev: true - /fast-check@3.10.0: - resolution: {integrity: sha512-I2FldZwnCbcY6iL+H0rp9m4D+O3PotuFu9FasWjMCzUedYHMP89/37JbSt6/n7Yq/IZmJDW0B2h30sPYdzrfzw==} + /fast-check@3.11.0: + resolution: {integrity: sha512-H2tctb7AGfFQfz+DEr3UWhJ3s47LXsGp5g3jeJr5tHjnf4xUvpArIqiwcDmL2EXiv+auLHIpF5MqaIpIKvpxiA==} engines: {node: '>=8.0.0'} dependencies: pure-rand: 6.0.1 diff --git a/src/Schema.ts b/src/Schema.ts index 5e273aa7a..8d33d0f30 100644 --- a/src/Schema.ts +++ b/src/Schema.ts @@ -2751,6 +2751,27 @@ export const UUID: Schema = 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 = pipe( + string, + pattern(ulidRegex, { + typeId: ULIDTypeId, + title: "ULID", + arbitrary: (): Arbitrary => (fc) => fc.ulid() + }) +) + /** * @category string * @since 1.0.0 diff --git a/test/Schema.ts b/test/Schema.ts index 74ebf7a6a..74805999b 100644 --- a/test/Schema.ts +++ b/test/Schema.ts @@ -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 diff --git a/test/data/filter/ULID.ts b/test/data/filter/ULID.ts new file mode 100644 index 000000000..f623d270b --- /dev/null +++ b/test/data/filter/ULID.ts @@ -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 ""` + ) + }) +})