-
-
Notifications
You must be signed in to change notification settings - Fork 405
Made Schema.brand not any-fy piped schemas #5360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 4ab07d6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 35 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
2266589
to
4ab07d6
Compare
If anybody else needs a dirty patch (because I didn't change source maps, and so all usages of "go to definition" in Schema file will be a bit shifted) for node_modules folder:
diff --git a/dist/dts/Schema.d.ts b/dist/dts/Schema.d.ts
index 15d098e7573a5f64135fdde3481fbba286514fb2..753d93b41b09ae09c73aa3e79ae2ee4574d1c741 100644
--- a/dist/dts/Schema.d.ts
+++ b/dist/dts/Schema.d.ts
@@ -1591,7 +1591,7 @@ export interface brand<S extends Schema.Any, B extends string | symbol> extends
* @category branding
* @since 3.10.0
*/
-export declare const brand: <S extends Schema.Any, B extends string | symbol>(brand: B, annotations?: Annotations.Schema<Schema.Type<S> & Brand<B>>) => (self: S) => brand<S, B>;
+export declare const brand: <S extends Schema.Any, B extends string | symbol>(brand: B, annotations?: Annotations.Schema<Schema.Type<S> & Brand<B>>) => <SubS extends S>(self: SubS) => brand<SubS, B>;
/**
* @category combinators
* @since 3.10.0
diff --git a/src/Schema.ts b/src/Schema.ts
index ab2e298242e0d2e8a93eceb0f6b7622efcf2a8f4..6e024c127e33f1ee024b45c18463470548ae804d 100644
--- a/src/Schema.ts
+++ b/src/Schema.ts
@@ -3403,7 +3403,7 @@ export const brand = <S extends Schema.Any, B extends string | symbol>(
brand: B,
annotations?: Annotations.Schema<Schema.Type<S> & Brand<B>>
) =>
-(self: S): brand<S, B> => {
+<SubS extends S>(self: SubS): brand<SubS, B> => {
const annotation: AST.BrandAnnotation = option_.match(AST.getBrandAnnotation(self.ast), {
onNone: () => [brand],
onSome: (brands) => [...brands, brand]
|
Hello @gcanti! May I please ask you for a few minutes of your time dedicated to the review of this tiny PR? I would greatly appreciate your attention and time, if you have the capacity and available resources for this effort. If you would like me to add it to |
@nikelborm actually I have been thinking about this for a few days, since it would be useful (for all APIs, not just for As for the implementation in this PR, it doesn't play well with annotations: import { pipe, Schema } from "effect"
const UserIdBrandSchema = Schema.brand("UserId", {
examples: ["a"] // UserIdBrandSchema should only apply to strings now
})
const UserIdSchema = pipe(
Schema.Number, // no error
UserIdBrandSchema
)
const UserIdSchema2 = pipe(
Schema.Number, // no error
Schema.brand("UserId", {
examples: ["a"]
})
) |
I noticed that too. The same thing is in const addAsdField = Schema.compose(Schema.Struct({
asd: Schema.String
}))
// addAsdField: (from: Schema.Schema.Any) => Schema.transform<Schema.Schema.Any, Schema.Struct<{
// asd: typeof Schema.String;
// }>>
// Has hardcoded Schema.Schema.Any here
const TestSchema = Schema.asSchema(pipe(
Schema.Struct({sdf: Schema.Number}),
addAsdField
))
// TestSchema: Schema.Schema<
// { readonly asd: string; },
// any,
// unknown
// > So it's generally applicable that there are 2 kinds of ways of writing generics. I noticed that some generics provide better inference in pipes, and some outside of pipes, and the user has to either choose, or there should be some clever thing that automatically determines if it is inside the pipe or outside. That's what I'm talking about: #4148 (comment) The important change is on lines 12-14: So in the end, could you please help me better understand how I interpret your response? I have a few interpretations in mind:
And thank you for the feedback! ❤️ |
Also, I figured there's even yet another thing that could be done: Writing tests to catch that case.
Because the CI says it's all clear, and it wasn't caught. |
As a baseline since const UserIdSchema2 = pipe(
Schema.Number,
Schema.brand("UserId", {
examples: ["a"] // should raise an error
})
) (I added a type-level test for this) Regarding the general issue, I have not found a way to achieve this behavior while also being able to define a generic function that applies a brand (within the same API). I have not found a solution for this in v4 either, although this is less of a problem there because brands are reified instead of being schema combinators, which makes them easier to reuse. Maybe I just have not found the right trick yet, any attempt to solve the issue is welcome. |
…n extracted to a constant
4ab07d6
to
725edda
Compare
* Fix effect peer dependency for bun-test Replace invalid "workspace:^" with "^3.17.9" and set release to patch (0.1.1) in changeset. * --example * Update AGENTS.md * Support native packages and update release config * Configure CI to publish only packages-native packages - Fix changeset ignore pattern from './packages/**/*' to '@effect/*' - Update release workflow to trigger on effect-native/main branch - Add native-specific build and publish scripts - Remove conflicting changeset with mixed packages - Add pre-push hook to prevent upstream contamination 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Test pre-push hook protection * Revert test hook change * Add comprehensive GitHub Copilot instructions for effect-native repository (#54) * add type level test for PR Effect-TS#5360 (Effect-TS#5435) * ensure Effect.promise captures span on defect (Effect-TS#5437) * Fix `InferenceConfiguration` schema in the Amazon Bedrock AI provider package (Effect-TS#5438) * Update GITHUB_TOKEN to use CHANGESETS_TOKEN * Relax effect peer dependency range * Remove bun peer dependency from bun-test * Create new-rivers-shake.md * Update package.json * Restrict workspace to native packages Remove packages/* entries and related tsconfig references. Update scripts to target only packages-native. Switch scratchpad deps and @effect/vitest to latest published versions. Add effect to changeset ignore list. * Delete pnpm-lock.yaml * lock ++ --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Giulio Canti <[email protected]> Co-authored-by: Tim <[email protected]> Co-authored-by: Maxwell Brown <[email protected]>
* Fix effect peer dependency for bun-test Replace invalid "workspace:^" with "^3.17.9" and set release to patch (0.1.1) in changeset. * --example * Update AGENTS.md * Support native packages and update release config * Configure CI to publish only packages-native packages - Fix changeset ignore pattern from './packages/**/*' to '@effect/*' - Update release workflow to trigger on effect-native/main branch - Add native-specific build and publish scripts - Remove conflicting changeset with mixed packages - Add pre-push hook to prevent upstream contamination 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Test pre-push hook protection * Revert test hook change * Add comprehensive GitHub Copilot instructions for effect-native repository (#54) * add type level test for PR Effect-TS#5360 (Effect-TS#5435) * ensure Effect.promise captures span on defect (Effect-TS#5437) * Fix `InferenceConfiguration` schema in the Amazon Bedrock AI provider package (Effect-TS#5438) * Update GITHUB_TOKEN to use CHANGESETS_TOKEN * Relax effect peer dependency range * Remove bun peer dependency from bun-test * Create new-rivers-shake.md * Update package.json * Restrict workspace to native packages Remove packages/* entries and related tsconfig references. Update scripts to target only packages-native. Switch scratchpad deps and @effect/vitest to latest published versions. Add effect to changeset ignore list. * Delete pnpm-lock.yaml * lock ++ * changesets configuration only ignores the scratchpad package, which actually exists in the workspace. The @effect/* and effect packages don't exist in this fork since we removed all the upstream packages from the workspace, so they shouldn't be in the ignore list. --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Giulio Canti <[email protected]> Co-authored-by: Tim <[email protected]> Co-authored-by: Maxwell Brown <[email protected]>
Type
Description
When
Schema.brand
was extracted into a variable, other schemas, piped through that variable, resulted inSchema.Any
type.Before
After