Handling Optional Nested Struct Validation Errors in Effect Typescript
Hello, is anyone know how to remove the undefined error of optional nested struct that is not obey the schema?
What I want is remove the error of
import { Schema } from "effect"
const Category = Schema.Struct({
type: Schema.String.pipe(Schema.minLength(10))
})
const Person = Schema.Struct({
name: Schema.String.pipe(Schema.minLength(5)),
age: Schema.Number.pipe(Schema.filter(value => value >= 18 || "Age should be greater than 18")),
category: Schema.optional(Category)
})
const data = { name: "", age: 5, category: { type: "" } }
console.log(
Schema.decodeUnknown(Person, { errors: "all", onExcessProperty: "ignore" })(data)
)
// result
{
"_id": "Either",
"_tag": "Left",
"left": {
"_id": "ParseError",
"message": "{ readonly name: minLength(5); readonly age: { number | filter }; readonly category?: { readonly type: minLength(10) } | undefined }\n├─ [\"name\"]\n│ └─ minLength(5)\n│ └─ Predicate refinement failure\n│ └─ Expected a string at least 5 character(s) long, actual \"\"\n├─ [\"age\"]\n│ └─ { number | filter }\n│ └─ Predicate refinement failure\n│ └─ Age should be greater than 18\n└─ [\"category\"]\n └─ { readonly type: minLength(10) } | undefined\n ├─ { readonly type: minLength(10) }\n │ └─ [\"type\"]\n │ └─ minLength(10)\n │ └─ Predicate refinement failure\n │ └─ Expected a string at least 10 character(s) long, actual \"\"\n └─ Expected undefined, actual {\"type\":\"\"}"
}
}import { Schema } from "effect"
const Category = Schema.Struct({
type: Schema.String.pipe(Schema.minLength(10))
})
const Person = Schema.Struct({
name: Schema.String.pipe(Schema.minLength(5)),
age: Schema.Number.pipe(Schema.filter(value => value >= 18 || "Age should be greater than 18")),
category: Schema.optional(Category)
})
const data = { name: "", age: 5, category: { type: "" } }
console.log(
Schema.decodeUnknown(Person, { errors: "all", onExcessProperty: "ignore" })(data)
)
// result
{
"_id": "Either",
"_tag": "Left",
"left": {
"_id": "ParseError",
"message": "{ readonly name: minLength(5); readonly age: { number | filter }; readonly category?: { readonly type: minLength(10) } | undefined }\n├─ [\"name\"]\n│ └─ minLength(5)\n│ └─ Predicate refinement failure\n│ └─ Expected a string at least 5 character(s) long, actual \"\"\n├─ [\"age\"]\n│ └─ { number | filter }\n│ └─ Predicate refinement failure\n│ └─ Age should be greater than 18\n└─ [\"category\"]\n └─ { readonly type: minLength(10) } | undefined\n ├─ { readonly type: minLength(10) }\n │ └─ [\"type\"]\n │ └─ minLength(10)\n │ └─ Predicate refinement failure\n │ └─ Expected a string at least 10 character(s) long, actual \"\"\n └─ Expected undefined, actual {\"type\":\"\"}"
}
}What I want is remove the error of
Expected undefined, actual {\"type\":\"\"}"Expected undefined, actual {\"type\":\"\"}" when categorycategory is not undefined