Typescript & simpl-schema - Making the move to TS and having basic questions

I think I have an entirely different approach, so let me share it as well.

No, it’s not redundant at all. As @rjdavid said, TypeScripts is for the programmer to prevent runtime errors (here: invalid data) from happening. Then the SimpleSchema is actually checking the data.

However, you don’t have to use SimpleSchema to do that. Even the Meteor-native check function may be enough in some cases, at least as the amount of validation you perform is relatively small. (It also lacks a proper error reporting and that may be needed, especially for the end-users.) If you’ll use @types/meteor, check becomes a handy type guard (source):

Meteor.methods({
  example(input: unknown) {
    // input: unknown
    check(input, {
      x: Match.Optional(Number),
      y: Number,
    });
    // input: { x?: number; y: number }
  },
});

I do use it quite extensively, especially when the input is sort of “internal”, i.e., I don’t care about the error message.

I do agree, it’s awesome to have them in sync. But it’s also tedious. Some time ago I’ve experimented with automatic type inference for SimpleSchema (TypeScript 4.3 and SimpleSchema), but I didn’t use it in the end. And that’s not because it won’t work, but because I’m trying not to use SimpleSchema at all.

When it comes to GraphQL, I most often go with the schema-first approach, use GraphQL Codegen to create TypeScript types, and then implement the already typed resolvers. (Just keep in mind, that the validation in GraphQL is very limited: no “minimum length”, “maximum value”, etc. You have to do it with custom in-DSL directives or directly in the resolvers.)

That’s a great one, really! There are so many great tools to work with JSON schema (including uniforms), that it’s not only easier to work with but also definitely more future-proof. If it matters for you, you can also share the schema with 3rd parties, like external integrators.

What is more, you can use the same JSON schema both in-app (for validation) and directly in-MongoDB (for ensuring data consistency). Of course, the latter is something that should never be needed, but it’s good to have it. Check out Using built-in MongoDB schemas for more.

That’s one of the advantages of cross-technology standards. I can also recommend doing the opposite, i.e., write code that generates both the types as well as the schema (and then use the schema to create a validator, using ajv). One tool I strongly recommend is typebox.

I also have used it for years. And probably will for a couple more, in some projects. But personally, I do switch to JSON schema whenever possible. Same goes for aldeed:collection2 and universe:collection - I try not to have any sort of ORM/ODM at all and handle the raw (helper-less) objects instead.

4 Likes