Skip to content

Commit

Permalink
refactor: align brands
Browse files Browse the repository at this point in the history
  • Loading branch information
patroza committed May 14, 2024
1 parent 47fcaea commit 24c3fe9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-falcons-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"effect-app": patch
"@effect-app/schema": patch
---

align brands
2 changes: 1 addition & 1 deletion packages/prelude/src/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ export interface UserProfileIdBrand extends Simplify<B.Brand<"UserProfileId"> &
* @tsplus type UserProfileId
*/
export type UserProfileId = StringId & UserProfileIdBrand
export const UserProfileId = brandedStringId<UserProfileIdBrand>()
export const UserProfileId = brandedStringId<UserProfileId>()
24 changes: 18 additions & 6 deletions packages/schema/src/moreStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { WithDefaults } from "./ext.js"
import { withDefaultConstructor, withDefaultMake } from "./ext.js"
import { type B } from "./schema.js"
import { NonEmptyString } from "./strings.js"
import type { NonEmptyString255Brand, NonEmptyString2k, NonEmptyStringBrand } from "./strings.js"
import type { NonEmptyString255Brand, NonEmptyStringBrand } from "./strings.js"

/**
* A string that is at least 1 character long and a maximum of 50.
Expand All @@ -28,7 +28,11 @@ export type NonEmptyString50 = string & NonEmptyString50Brand
*/
export const NonEmptyString50 = NonEmptyString.pipe(
S.maxLength(50),
fromBrand(nominal<NonEmptyString50>(), { identifier: "NonEmptyString50", title: "NonEmptyString50", jsonSchema: {} }),
fromBrand(nominal<NonEmptyString50>(), {
identifier: "NonEmptyString50",
title: "NonEmptyString50",
jsonSchema: {}
}),
withDefaultMake
)

Expand All @@ -47,7 +51,11 @@ export type NonEmptyString64 = string & NonEmptyString64Brand
*/
export const NonEmptyString64 = NonEmptyString.pipe(
S.maxLength(64),
fromBrand(nominal<NonEmptyString64>(), { identifier: "NonEmptyString64", title: "NonEmptyString64", jsonSchema: {} }),
fromBrand(nominal<NonEmptyString64>(), {
identifier: "NonEmptyString64",
title: "NonEmptyString64",
jsonSchema: {}
}),
withDefaultMake
)

Expand All @@ -67,7 +75,11 @@ export type NonEmptyString80 = string & NonEmptyString80Brand

export const NonEmptyString80 = NonEmptyString.pipe(
S.maxLength(80),
fromBrand(nominal<NonEmptyString80>(), { identifier: "NonEmptyString80", title: "NonEmptyString80", jsonSchema: {} }),
fromBrand(nominal<NonEmptyString80>(), {
identifier: "NonEmptyString80",
title: "NonEmptyString80",
jsonSchema: {}
}),
withDefaultMake
)

Expand Down Expand Up @@ -111,7 +123,7 @@ export const Min3String255 = pipe(
S.String,
S.minLength(3),
S.maxLength(255),
fromBrand(nominal<NonEmptyString2k>(), { identifier: "Min3String255", title: "Min3String255", jsonSchema: {} }),
fromBrand(nominal<Min3String255>(), { identifier: "Min3String255", title: "Min3String255", jsonSchema: {} }),
withDefaultMake
)

Expand Down Expand Up @@ -143,7 +155,7 @@ export const StringId = extendM(
S.String,
S.minLength(minLength),
S.maxLength(maxLength),
fromBrand(nominal<StringIdBrand>(), {
fromBrand(nominal<StringId>(), {
identifier: "StringId",
title: "StringId",
arbitrary: StringIdArb,
Expand Down
20 changes: 10 additions & 10 deletions packages/schema/src/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ export interface PositiveIntBrand
export const PositiveInt = extendM(
S.Int.pipe(
S.positive(),
fromBrand(nominal<PositiveIntBrand>(), { identifier: "PositiveInt", title: "PositiveInt", jsonSchema: {} }),
fromBrand(nominal<PositiveInt>(), { identifier: "PositiveInt", title: "PositiveInt", jsonSchema: {} }),
withDefaultMake
),
(s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(1))) })
)
export type PositiveInt = S.Schema.Type<typeof PositiveInt>
export type PositiveInt = number & PositiveIntBrand

export interface NonNegativeIntBrand extends Simplify<B.Brand<"NonNegativeInt"> & IntBrand & NonNegativeNumberBrand> {}
export const NonNegativeInt = extendM(
S.Int.pipe(
S.nonNegative(),
fromBrand(nominal<NonNegativeIntBrand>(), {
fromBrand(nominal<NonNegativeInt>(), {
identifier: "NonNegativeInt",
title: "NonNegativeInt",
jsonSchema: {}
Expand All @@ -31,20 +31,20 @@ export const NonNegativeInt = extendM(
),
(s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(0))) })
)
export type NonNegativeInt = S.Schema.Type<typeof NonNegativeInt>
export type NonNegativeInt = number & NonNegativeIntBrand

export interface IntBrand extends Simplify<B.Brand<"Int">> {}
export const Int = extendM(
S.Int.pipe(fromBrand(nominal<IntBrand>(), { identifier: "Int", title: "Int", jsonSchema: {} }), withDefaultMake),
S.Int.pipe(fromBrand(nominal<Int>(), { identifier: "Int", title: "Int", jsonSchema: {} }), withDefaultMake),
(s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(0))) })
)
export type Int = S.Schema.Type<typeof Int>
export type Int = number & IntBrand

export interface PositiveNumberBrand extends Simplify<B.Brand<"PositiveNumber"> & NonNegativeNumberBrand> {}
export const PositiveNumber = extendM(
S.Number.pipe(
S.positive(),
fromBrand(nominal<PositiveNumberBrand>(), {
fromBrand(nominal<PositiveNumber>(), {
identifier: "PositiveNumber",
title: "PositiveNumber",
jsonSchema: {}
Expand All @@ -53,15 +53,15 @@ export const PositiveNumber = extendM(
),
(s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(1))) })
)
export type PositiveNumber = S.Schema.Type<typeof PositiveNumber>
export type PositiveNumber = number & PositiveNumberBrand

export interface NonNegativeNumberBrand extends Simplify<B.Brand<"NonNegativeNumber">> {}
export const NonNegativeNumber = extendM(
S
.Number
.pipe(
S.nonNegative(),
fromBrand(nominal<NonNegativeNumberBrand>(), {
fromBrand(nominal<NonNegativeNumber>(), {
identifier: "NonNegativeNumber",
title: "NonNegativeNumber",
jsonSchema: {}
Expand All @@ -70,7 +70,7 @@ export const NonNegativeNumber = extendM(
),
(s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(0))) })
)
export type NonNegativeNumber = S.Schema.Type<typeof NonNegativeNumber>
export type NonNegativeNumber = number & NonNegativeNumberBrand

/** @deprecated Not an actual decimal */
export const NonNegativeDecimal = NonNegativeNumber
Expand Down
6 changes: 5 additions & 1 deletion packages/schema/src/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export type NonEmptyStringBrand = B.Brand<"NonEmptyString">
export type NonEmptyString = string & NonEmptyStringBrand
export const NonEmptyString = nonEmptyString
.pipe(
fromBrand(nominal<NonEmptyString>(), { identifier: "NonEmptyString", title: "NonEmptyString", jsonSchema: {} }),
fromBrand(nominal<NonEmptyString>(), {
identifier: "NonEmptyString",
title: "NonEmptyString",
jsonSchema: {}
}),
withDefaultMake
)

Expand Down

0 comments on commit 24c3fe9

Please sign in to comment.