A
arktype•7mo ago
ciscoheat

Problem with validation in 2.0 dev?

I'm using 2.0.0-dev.11 and cannot figure out why some very basic validation fails. Short example:
import { type } from "arktype";

const nospacePattern = /^\S*$/;

const schema = type({
name: "string",
email: "email",
tags: "(string>=2)[]>=3",
score: "integer>=0",
"date?": "Date",
"nospace?": nospacePattern,
extra: "string|null",
});

const data = {
name: "Ok",
email: "",
tags: ["AB", "B"],
score: -1,
date: undefined,
nospace: "One space",
};

const out = schema(data);

console.log("nospace matches:", nospacePattern.test(data.nospace));

if (out instanceof type.errors) {
console.log(out.summary);
} else {
console.log(out);
}
import { type } from "arktype";

const nospacePattern = /^\S*$/;

const schema = type({
name: "string",
email: "email",
tags: "(string>=2)[]>=3",
score: "integer>=0",
"date?": "Date",
"nospace?": nospacePattern,
extra: "string|null",
});

const data = {
name: "Ok",
email: "",
tags: ["AB", "B"],
score: -1,
date: undefined,
nospace: "One space",
};

const out = schema(data);

console.log("nospace matches:", nospacePattern.test(data.nospace));

if (out instanceof type.errors) {
console.log(out.summary);
} else {
console.log(out);
}
The output I get from running this is:
nospace matches: false
email must be a valid email (was "")
extra must be a string or null (was missing)
date must be a Date (was undefined)
nospace matches: false
email must be a valid email (was "")
extra must be a string or null (was missing)
date must be a Date (was undefined)
I'd expected errors for score, nospace, tags and maybe tags[1] as well. Anything very simple I'm missing?
11 Replies
ciscoheat
ciscoheatOP•7mo ago
If I remove some fields from the schema:
const schema = type({
name: "string",
tags: "(string>=2)[]>=3",
score: "integer>=0",
"date?": "Date",
"nospace?": nospacePattern,
});
const schema = type({
name: "string",
tags: "(string>=2)[]>=3",
score: "integer>=0",
"date?": "Date",
"nospace?": nospacePattern,
});
The score error appears:
nospace matches: false
score must be at least 0 (was -1)
date must be a Date (was undefined)
nospace matches: false
score must be at least 0 (was -1)
date must be a Date (was undefined)
And when the schema looks like this, the tags error appear:
const schema = type({
tags: "(string>=2)[]>=3",
"date?": "Date",
"nospace?": nospacePattern,
});
const schema = type({
tags: "(string>=2)[]>=3",
"date?": "Date",
"nospace?": nospacePattern,
});
nospace matches: false
tags must be at least length 3 (was 2)
date must be a Date (was undefined)
nospace matches: false
tags must be at least length 3 (was 2)
date must be a Date (was undefined)
ciscoheat
ciscoheatOP•7mo ago
Seems like there is an error count in traversal.ts that gets triggered and doesn't include some errors: https://github.com/arktypeio/arktype/blob/08b151eb07f9704080cc68db7e3ab551fafca368/ark/schema/shared/traversal.ts#L107
GitHub
arktype/ark/schema/shared/traversal.ts at 08b151eb07f9704080cc68db7...
TypeScript's 1:1 validator, optimized from editor to runtime - arktypeio/arktype
ciscoheat
ciscoheatOP•7mo ago
This is what the score validator looks like:
intersection39Apply(data, ctx) {
this.domain5Apply(data, ctx) // Checking if type is a number
if (ctx.hasError()) {
return // Exits here, since a previous error exists
}
this.divisor1Apply(data, ctx)
if (ctx.failFast && ctx.hasError()) {
return
}
this.min1Apply(data, ctx)
}
intersection39Apply(data, ctx) {
this.domain5Apply(data, ctx) // Checking if type is a number
if (ctx.hasError()) {
return // Exits here, since a previous error exists
}
this.divisor1Apply(data, ctx)
if (ctx.failFast && ctx.hasError()) {
return
}
this.min1Apply(data, ctx)
}
TizzySaurus
TizzySaurus•7mo ago
For date, passing in a value of undefined is indeed incorrect from my understanding. Either it should be a valid date or not provided. Not providing and providing as undefined are two different things You'd need date: "Date|undefined" or "date?": "Date|undefined" if the key can be missing (again, set to undefined isn't the same as missing)
ciscoheat
ciscoheatOP•7mo ago
Probably yes, but all errors should be reported anyway @ssalbdivad sorry to bother you, it's just that I'd really like 2.0 to work with Superforms. 😌 But it seems like there is a ctx.failFast check missing in the above code? Since for form validation, it's essential to have all errors reported.
TizzySaurus
TizzySaurus•7mo ago
Ah, I see, you're pointing out not all errors are raised Yeah, that seems like a bug
ciscoheat
ciscoheatOP•7mo ago
Yes, but what you say is interesting as well. I don't know if there is an in validator in ArkType, otherwise it could be the same (as a missing key is undefined), but I don't know.
ssalbdivad
ssalbdivad•7mo ago
Thanks for the detailed info here, I recently made some changes to how structure nodes are compiled to accomodate index types and this is definitely a bug resulting from that. Will release a new version imminently to resolve
ciscoheat
ciscoheatOP•7mo ago
Nice, thank you very much 🙂
ssalbdivad
ssalbdivad•7mo ago
Try 2.0.0-dev.12 when you get a chance 🙂
ciscoheat
ciscoheatOP•7mo ago
Whohoo, worked directly!
Want results from more Discord servers?
Add your server