Closed halfcadence closed 1 year ago
Hi @halfcadence !
You are right, it's not possible right now to use the property name in deserialization patterns. I'll look into that, it could be an interesting feature.
Meanwhile, there's a very simple trick you can use (that may even be cleaner than using the property name): JSON schemas are extensible, so you can use a custom internalId
prop:
const schemaWithCustomStyle = {
type: "object",
properties: {
customStyle: {
internalId: "customStyle",
type: "object",
},
},
additionalProperties: false,
} as const;
type TypeWithCustomStyle = FromSchema<
typeof schemaWithCustomStyle,
{
deserialize: [
{
pattern: {
internalId: "customStyle";
};
output: CustomStyle;
}
];
}
>;
Thanks for the comment, that makes sense and I think would solve this problem.
(As a different issue, I realized that the schema I'm working with is recursive and so isn't supported for that reason)
I'm trying to use a custom deserialization to type check CSS in my JSON objects.
I'm using a CSS type from
csstype
:And have the CSS as a property called "customStyle" in my schemas:
The type I would like to get from this is something like
However since there's not enough information in the type to match a pattern:
matches the outer schema:
Is there a way to pattern match based on the name of the property, e.g.
I took a look at the
deserialize.ts
file:but don't understand how exactly it's working.