string-store input validation

Hi, is there a way I could make a string-store Schema validate its input before serializing, or would I need a separate schema (eg zod) for that?
9 Replies
kyra
kyra2w ago
It is impossible to do that considering string-store serialises similarly to the smallest representation of an object in memory without RTTI, there’s simply no information for that And given arbitrary input, it’ll give you an error if it’s too short, or give you garbage (or an error) data otherwise, you would need to RTTI into it but even then it can still give you garbage data.
Amgelo
AmgeloOP2w ago
I'm not sure I explained myself well, what I meant is this:
const schema = new Schema(0).string('foo');
// both would throw
schema.serialize({});
schema.serialize({ foo: 1 });
const schema = new Schema(0).string('foo');
// both would throw
schema.serialize({});
schema.serialize({ foo: 1 });
maybe even for the deserialization part as well, given one can extract the id, then I can know the schema it should follow but I meant mostly about the serialization part
kyra
kyra2w ago
Oh, ok, note to self do not reply while commuting at 7:30 in the morning My answer was for deserialization, for serialization I could add some validations
Amgelo
AmgeloOP2w ago
yeah for serialization it would be useful, mostly because of possible js users lol I do wonder why it isn't possible for deserialization though like, wouldn't it be able to, say, throw if it deserializes a 2 in a bit field type?
kyra
kyra2w ago
A 2 in a bitfield type is 0b10 😅 Data is stored in binary format using the full range of UTF16 The way languages like JavaScript know what kind of object a region is, is because the memory has tags saying "hey, this is a string", "hey, this is a boolean", etc. string-store doesn't have RTTI, it's basically equivalent to doing reinterpret_cast<MyStructure*>(&u16buffer), it just assumes
Amgelo
AmgeloOP2w ago
so the way the data is interpreted depends on the schema?
kyra
kyra2w ago
Yes The lack of RTTI makes it very memory efficient And I could make an option to make string-store insert RTTI, but it would still fail to validate correctly given arbitrary user input while deserialising
Amgelo
AmgeloOP2w ago
hmm I see in that case yeah it seems not worth the trouble I think it'd be useful to have it for serialization though I was just writing my tests and was surprised it didn't throw lol
kyra
kyra2w ago
Yeah, I can make some checks

Did you find this page helpful?