Show HN: I built a request payload validator for JavaScript based server
github.comThe issue I faced writing JS based Server is the amount of validation that goes behind request payload. Validation includes
- if the key is present
- if the value of the key is in given range (any filter function you can imagine)
- conditional key presence (Ex: if key1 is present key2 should be less than 10)
- complex nested structure of the payload
- array validation (length / type / property)
I end up write a significant amount of if else block just to validate if the payload is acceptable. So I wrote a tool a long time back which I use in all my JS based server project (My gateway servers are always in Node for past 3-4 years because of the amount of instrumentation / ease of debugging)
I wrote helson 2 years back, where a dev would define schema of a payload
``` typedef Payload { str "url": pass, []str "tags": pass, bool "isCollection": pass, obj "content": { str "body": strShouldNotBeEmpty | shouldBeOfMinimumLength 15 | shouldHaveMinimumWordLength 5, }, } ```
with primitive type support of str, bool, number and compound type support array, enum, ref (https://github.com/adotg/helson/blob/develop/test/helson.tes...) for type checking and custom function (strShouldNotBeEmpty, shouldBeOfMinimumLength above example) as value checking
And it tests against incoming payload
``` { url: '#/e/hash-of-a-link', tags: ['a1', 'a2', 'a3'], content: { body: 'This is a body', } ```
Now that I use typescript, I was in the verge of deciding (I don't have enough time solving just for myself as the value addition is not justified for myself, but if enough people wants it I'll build it) should I also build a typescript supported workflow. Like from a schema like above generate interfaces (and vice versa). Or is this lib meaningful to you at all.
It also throws meaningful error automatically (which you can override) to return to client directly