Dawson
Dawson
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
all that and I need to use superRefine anyway as that's the only way I can abort early 😩 https://github.com/colinhacks/zod/issues/3884
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
found the culprit! the way we set up otel, specifically with zone.js, is screwing with the promise prototype for async function returns – removing the context manager line fixes the issue:
const provider = new WebTracerProvider({ resource })

provider.addSpanProcessor(
new BatchSpanProcessor(
new OTLPTraceExporter({
url: import.meta.env.VITE_OTLP_TRACE_EXPORTER_URL,
}),
),
)

provider.register({
propagator: new W3CTraceContextPropagator(),
contextManager: new ZoneContextManager(), // THIS LINE
})
const provider = new WebTracerProvider({ resource })

provider.addSpanProcessor(
new BatchSpanProcessor(
new OTLPTraceExporter({
url: import.meta.env.VITE_OTLP_TRACE_EXPORTER_URL,
}),
),
)

provider.register({
propagator: new W3CTraceContextPropagator(),
contextManager: new ZoneContextManager(), // THIS LINE
})
- https://github.com/open-telemetry/opentelemetry-js/issues/3030 - https://github.com/angular/angular/issues/51328#issuecomment-1688604566 otel's ZoneContextManager seems to not support es2017+ from what I'm reading
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
the instanceof Promise check is apparently sometimes faulty depending on transpilation stuff or if we're using a custom library for promises – not sure what it is for me, but it seems to be the case that I'm not using a native promise. regardless, would it be more helpful for zod to simply check whether the result is "thenable"? as it's not doing anything more sophisticated under the hood, and that's more in line with the Promises/A+ spec: https://promisesaplus.com/#point-53 I can get a small PR up to show what I mean if that's an option
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
I'll update here as I find things out, but at a certain point I'll need to move on just use superRefine instead :sadkragg:
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
I tracked down the mismatch to this condition in the refine logic: https://github.com/colinhacks/zod/blob/f7ad26147ba291cb3fb257545972a8e00e767470/src/types.ts#L381C45-L381C70 with this code, my project logs false in devtools but everywhere else it seems to log true (repl, codesandbox, etc.):
console.log((async () => false)() instanceof Promise)
console.log((async () => false)() instanceof Promise)
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
I use RHF elsewhere but I've been testing this outside of that usage – I'll see if I can get a barebones react repro up somewhere
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
some notes: - the issue happens whether the statement is executed in a react component/hook body or at the file top-level - I'm running the node REPL with yarn node so it should be using the same zod version - I'm running vite in dev mode with HMR and all that, so I'll try a production build and see what happens
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
caution: strange behavior ahead 😅 I get expected behavior in the node REPL:
➜ yarn node
Welcome to Node.js v22.9.0.
Type ".help" for more information.
> const {z} = require("zod")
undefined
> z.string().refine(async name => false).safeParseAsync('test').then(console.log)
Promise {
<pending>,
[Symbol(async_id_symbol)]: 57,
[Symbol(trigger_async_id_symbol)]: 53
}
> { success: false, error: [Getter] }
➜ yarn node
Welcome to Node.js v22.9.0.
Type ".help" for more information.
> const {z} = require("zod")
undefined
> z.string().refine(async name => false).safeParseAsync('test').then(console.log)
Promise {
<pending>,
[Symbol(async_id_symbol)]: 57,
[Symbol(trigger_async_id_symbol)]: 53
}
> { success: false, error: [Getter] }
but running the exact same code in my vite + react project I get the incorrect log in the devtools console:
{success: true, data: 'test'}
{success: true, data: 'test'}
so seemingly not an issue with zod but maybe with vite or react with zod
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
ok so it's working with superRefine, so maybe I'll use that – am I using refine wrong or is this a bug?
z.string()
.min(1)
.superRefine(async (name, ctx) => {
ctx.addIssue({
code: 'custom',
})
return false
// const result = await checkNameAvailability(name)
// return !!result?.isAvailable
})
.safeParseAsync('test')
.then(console.log)
z.string()
.min(1)
.superRefine(async (name, ctx) => {
ctx.addIssue({
code: 'custom',
})
return false
// const result = await checkNameAvailability(name)
// return !!result?.isAvailable
})
.safeParseAsync('test')
.then(console.log)
logs {success: false}
27 replies
ZZod
Created by Dawson on 12/18/2024 in #questions
Dawson - Can someone sanity check this for me? ...
simply removing the async keyword gives an expected result, but I obviously can't do that if I need an async call to perform the validation
27 replies