1. 23
  1.  

  2. 4

    Getting to your let and ref features is interesting because it makes me then ask, why is this specified in JSON at all? “So it can manipulate/specify itself” is a pretty reasonable answer, but being able to rename/etc symbols means that a structure fitting the schema no longer is enough to tell if it’s valid, you have to do some processing to know whether the structure is truly valid. So it is no longer “just” JSON, you are defining a more powerful language that just uses JSON as its syntax. (Sorry for the wobbly explanation; there’s probably some formal name for this property but I am still processing coffee.)

    For example, you can define this type but can never write an instance of it:

    {
      "let": {
        "Veggie": {
          "veggieName": "string",
          "veggieLike": "boolean",
          "ohNo": ["ref", "Veggie"]
        }
      },
      "schema": {
        "fruits": ["array", "string"],
        "vegetables": ["array", ["ref", "Veggie"]]
      }
    }
    

    Basically, that feature adds power in a BIG way and it’s interesting how stark it is. I guess normal JSON schemas have the same feature, but still.

    Also having the version number be optional gives me the heebie-jeebies, but that’s just me being paranoid.

    1. 4

      Excellent, this is very close to something I was planning to attempt, you may have saved me a whole gang of time :)

      Question - in the personSchema example, is there a way I can extract and expose the type being described? Like export type Person = something<personSchema> ?

      1. 6

        Yes, the MatchesSchema type is described in the API but I didn’t include an example of using it this way. I probably should. It works like this:

        import { MatchesSchema } from 'spartan-schema';
        
        export type Person = MatchesSchema<typeof personSchema>;
        
      2. 3

        Well considering I had only just discovered Zod, this is really interesting.

        1. 2

          Interesting, I hadn’t heard of Zod before. I guess the main difference between it and Spartan Schema is that Zod is just a Typescript API (using function calls), whereas Spartan Schema is a JSON language with a Typescript library.