Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Schema Validation #4

Closed
2 tasks
ComBarnea opened this issue Sep 1, 2021 · 5 comments · Fixed by #91
Closed
2 tasks

Template Schema Validation #4

ComBarnea opened this issue Sep 1, 2021 · 5 comments · Fixed by #91
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed
Milestone

Comments

@ComBarnea
Copy link
Collaborator

  • I'm submitting a ...
    [ ] bug report
    [X] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary
    Add for variable schema for templates

  • Add option for variable validation using Joi, Yup, class-validator, and such

  • Add option for a variable to fall back if missing of failing validation

  • Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)

@ComBarnea ComBarnea added this to the Version 0.1.0 milestone Sep 1, 2021
@scopsy scopsy removed this from the Version 0.1.0 milestone Sep 15, 2021
@scopsy scopsy added the enhancement New feature or request label Sep 15, 2021
@ComBarnea ComBarnea added this to the Version 0.1.1 milestone Sep 25, 2021
@davidsoderberg
Copy link
Contributor

@scopsy do you guys mind if I dig my head into this issue?

@ComBarnea
Copy link
Collaborator Author

ComBarnea commented Nov 3, 2021

@davidsoderberg sure thing, assigning this to you right now, @scopsy do you have anything else to add here?

Screen Shot 2021-11-03 at 19 39 09

---- Lets assume this didn't happen 😆

@scopsy
Copy link
Contributor

scopsy commented Nov 3, 2021

@davidsoderberg sure! You can outline and API for the validations so we can discuss it with the community?

Some initial thoughts: we can create an API that will allow any validation system to work using some sort of plugin approach. This can support validators like Joi or class validators. Or just a simple function validation with custom logic.

@ComBarnea ComBarnea added the help wanted Extra attention is needed label Nov 3, 2021
@davidsoderberg
Copy link
Contributor

I was thinking of a function that can be a promise and then we do not catch it so the error can be catched by the user of the package. Any thoughts on that?

@scopsy
Copy link
Contributor

scopsy commented Nov 3, 2021

@davidsoderberg Yeah! This definitely can be a promise. I've attached some ideas for making a pluggable system with bringing your own validator library style. Let me know what you think:

This can be the validator schema, the main thing is that each validator should return a validate function, the implementation doesn't matter as long as the validate method exists.

interface ITemplateSchema {
   validate(payload: ITriggerPayload): Promise<boolean>;
}

This is one implementation for JOI, we can use similar once to validate with other validator libraries or custom code.

class JoiValidator implements ITemplateSchema {
    constructor(private schema: JoiSchema) {

    }

    async validate(payload: ITrigger) {
      await this.schema.validateAsync(payload);
      return true;
   }
}

Not sure about the schema key here maybe we can find a better name?
And I think this should be on the template level or the message level?

const passwordResetTemplate = await notifire.registerTemplate({
  id: 'password-reset',
  schema: new JoiValidator(Joi.object({
    username: Joi.string()
        .alphanum()
        .min(3)
        .max(30)
        .required()
   })),
  messages: [
    {
      subject: 'Your password reset request',
      channel: ChannelTypeEnum.EMAIL,
      template: `
          Hi {{firstName}}!
      `
    },
  ]
});

@scopsy scopsy linked a pull request Nov 4, 2021 that will close this issue
@scopsy scopsy closed this as completed Nov 4, 2021
scopsy pushed a commit that referenced this issue Jan 3, 2022
ainouzgali pushed a commit that referenced this issue Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants