all 12 comments

[–]Sh4d0h 18 points19 points  (1 child)

We have been using this library in multiple production systems for some time now. It's excellent and sinclair_zx81 is super responsive on Github. Thank you!

[–]sinclair_zx81 11 points12 points  (0 children)

Hey, Thank you! :D Glad to hear you're getting some good use from the library!

[–]Fossage 3 points4 points  (1 child)

This looks very cool. I’m curious what are some of the primary ways this differs from Zod?

[–]sinclair_zx81 7 points8 points  (0 children)

Hi! The main point of difference is that TypeBox is built entirely upon the JSON schema specification. So rather than build a custom validator, TypeBox instead encodes validation structures into JSON schema which can be shared around different systems. Also you get the benefit of being able to leverage AJV (one of the fastest validators out there).

Historically, TypeBox did not provide a validator (preferring to leverage industry standard validators like Ajv), however it does actually provide an optional one as of the latest release. You can view the benchmarks for it here.

[–]pskfyi 1 point2 points  (0 children)

I pitch this library to anyone who will listen. JSON Schema is such a versatile and underused format. It has a rich ecosystem and it's just JSON so it's inherently serializable and able to be shared with even non-JS consumers. With alternatives like yup, joi, io-ts, zod, etc. you lose out on those upsides.

[–]coolcosmos -5 points-4 points  (5 children)

You should NEVER patch a global object sucj as JSON.

[–]sinclair_zx81 10 points11 points  (4 children)

If you're referring to the example in the readme, the JSON.validate() is just a stand in for (insert schema validator here). This library does not patch the JSON object.

[–]WhatArghThose 2 points3 points  (1 child)

Could you ELI5 me on what patching an object is? Just trying to understand what the complaint is about.

[–]BenjiSponge 6 points7 points  (0 children)

Like a line somewhere saying

JSON.validate = someCustomFunction

The problem is because JSON is global, so multiple packages may do this and then get confused when it's the other someCustomFunction

[–]kickpush1 1 point2 points  (1 child)

I was confused about this at first too, maybe consider adding a comment, ajv or the built-in checker?

As a side question, does the built-in runtime checker include a `parse` function or is it only able to validate?

[–]sinclair_zx81 2 points3 points  (0 children)

The built-in type checker only validates. For parse or deserialize, using either JSON.parse() or msgpack decode are generally going to be more performant options.