by staticassertion · 5y ago · 0 comments
ie:
fn safe_read(path: str) -> Tainted<IO> { Tainted(unsafe_read(path)) }
And then you can apply functions to Tainted<IO> or whatever type that convert it into something structured / validated.
So long as your functions only take in those validated types (ie: you do not write functions that take str) you can ensure that new reads will fail to typcheck without first parsing.
To be honest this is how most programs I see work anyways, at least in typed languages. Few work directly on strings. But they do it naturally, without enforcement - so like, a function might take a 'str', but the 'str' passed in was parsed into a wrapping structure already.
For example, you can convert the input into a safe representation, suitable for the exact place you'll be using the string, instead of "validating" it.
What you need is a safe type for each use case, and ways to convert values to that (or mark them as that depending on your TS).
> Few work directly on strings. But they do it naturally, without enforcement - so like, a function might take a 'str', but the 'str' passed in was parsed into a wrapping structure already.
ie: Most programs in typed languages already do what you're saying - they parse the data directly into a structure, and therefor they validate some aspects of it naturally, so even when you do see a 'str' in typed code it's very often already gone through some sort of parsing phase.
You don't sanitize user input when parsing, you do it on usage. "Robert'; drop table users--" is a perfectly fine name and you shouldn't mangle it on you data fields.