hschmaiske:meteor-settings-validator

v1.0.1Published 9 months ago

Meteor Settings Validator

The MeteorSettingsValidator class simplifies the validation of Meteor.settings using a specified Zod schema. This utility ensures that the settings adhere to the expected structure and provides a straightforward method, get(), to fetch and utilize them in a TypeScript project.

How it works

The class is tailored for use with Zod, a TypeScript-centric schema declaration and validation library. It takes a ZodObject representing the anticipated shape of Meteor.settings and furnishes a get() method to fetch and validate the settings.

Upon successful validation, the method returns the verified settings. If executed on the client side, it exclusively returns the public settings to bolster security.

Usage

  1. Installation: Ensure Zod and meteor-settings-validator are installed in your project.
meteor add hschmaiske:meteor-settings-validator
meteor npm i zod
  1. Import the class: Import the MeteorSettingsValidator class into your TypeScript file.
1import { MeteorSettingsValidator } from "hschmaiske:meteor-settings-validator";
  1. Create a ZodObject for Meteor.settings: Define a ZodObject that represents the expected structure of Meteor.settings. For example:
1const MeteorSettingsSchema = z.object({
2  public: z.object({
3    // Define your public settings structure here
4  }),
5  // Add other properties as needed
6});
  1. Instantiate the Validator: Create an instance of MeteorSettingsValidator with your ZodObject.
1const settingsValidator = new MeteorSettingsValidator(MeteorSettingsSchema);
  1. Retrieve and Use Meteor.settings:
  • Call the get() method to retrieve and validate Meteor.settings.
1const settingsValidator = new MeteorSettingsValidator(MeteorSettingsSchema);
2const settings = settingsValidator.get();
3
4// and you can use like this:
5settings.public.myCustomKey
  • TODO: add tests