npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mw.validation

v1.0.15

Published

Declarative validation fully customisable that aim to normalize error message for design-systems

Downloads

243

Readme

mw.validation

This librarie is able to validate an entire javascript Object or just properties. The main features:

  • The syntaxe is human readable.

  • You can still validate with the code (the phylosophy is; you will ever have specific validation)

  • Full object validation

  • Single property object validation from 2 side

    • Single propery validation from a view (everything is a string) https://www.guillaume-chervet.fr/articles/item/0548276c-4209-4b8f-beb8-da9067840248/encore-un-peu-de-validation
      • will require "parsing"
    • Single property validation from an object to a view
      • will require "formatting"
  • Pre-customize rules :

    • url
    • require
    • digit: represent an integer
    • string
    • color
    • minLength
    • maxLength
    • date
    • etc. //TODO

How install

//Install it with :
- npm install mw.validation --save

Packages

How to use it server side (node.js)

For server side, inside node.js you can use Object validation:

var validation = require("mw.validation");

var model = {
  id: "3",
  name: "Ploufragan",
  author: "Guillaume",
  review: 3,
  image: {
    url:
      "http://localhost:8081/api/files/7577fcc0-0580-11e7-a2b8-5dcb02604871_hackathon.PNG",
    title: ""
  }
};

const rules = {
  id: ["required"],
  name: [
    "required",
    {
      minLength: {
        minLength: 3
      }
    },
    {
      maxLength: {
        maxLength: 100
        message: "Expression is too long {maxLength} characters maximum"
      }
    },
    {
      pattern: {
        regex: /^[a-zA-Z -]*$/
      }
    }
  ],
  author: ["required"],
  review: ["required", "digit"],
  "@image": {
    url: ["url"],
    title: [
      {
        required: {
          onlyIf: onlyIf,
          message: "Field Image title is required"
        }
      }
    ]
  }
};
var validationResult = validation.objectValidation.validateModel(
  newPlace,
  rules,
  true
);

if (!validationResult.success) {
  console.log(validationResult.detail);
}

Object validation samples

    (function () {
      var model = {
        id: "3",
        name: "Ploufragan",
        author: "Guillaume",
        review: 3,
        image: {
          url: "http://localhost:8081/api/files/7577fcc0-0580-11e7-a2b8-5dcb02604871_hackathon.PNG",
          title: ""
        }
      };

      var rules = {
        id: ['required'],
        name: ['required'],
        author: ['required'],
        review: ['required', 'digit'],
        '@image': {
          url: ['url'],
          title: ['required']
        }
      };

      var result = mw.objectValidation.validateModel(model, rules);

      console.log(JSON.stringify(result));
      // {"success":false,"detail":{"model.image.url.url":"Veuillez saisir une url valide.","model.image.title.required":"Le champ est requis."}}

      var model = {
        id: "3",
        name: "sd",
        author: "Guillaume",
        review: 3,
        image: {
          url: "",
          title: ""
        },
        youhou: ''
      };

      var result2 = mw.objectValidation.validateModel(model, rules, true);

      console.log(JSON.stringify(result2));
      // {"success":false,"detail":{"model.youhou.illegal":"La proprieté n'est pas authorisée.","model.image.title.required":"Le champ est requis."}}

      var onlyIf = function(){
         if(model.image && model.image.url){
           return true;
         }
         return false;
      }

        var rules3 = {
        id: ['required'],
        name: ['required', {minLength: { minLength: 3}}, { maxLength : { maxLength: 100}}, {pattern: { regex: /^[a-zA-Z -]*$/}} ],
        author: ['required'],
        review: ['required', 'digit'],
        '@image': {
          url: ['url'],
          title: [ { required: { onlyIf:onlyIf, message: 'Field Image title is required' }}]
        }
      };
       var result3 = mw.objectValidation.validateModel(model, rules3, true);
      console.log(JSON.stringify(result3));
      // {"success":false,"detail":{"model.name.minLength":"Veuillez saisir au moins 3 caractère(s).","model.youhou.illegal":"La proprieté n'est pas authorisée."}}

       var model = {
        id: "3",
        name: "sd",
        author: "Guillaume",
      };

       var result4 = mw.objectValidation.validateModel(model, rules3, true);
      console.log(JSON.stringify(result4));
      // {"success":false,"detail":{"model.name.minLength":"Veuillez saisir au moins 3 caractère(s).","model.review.notfound":"La proprieté n'est pas présente.","model.image.notfound":"La proprieté n'est pas présente."}}

View validation (generaly used at client side)

View validation validate only one property from a list of rules.

var rules = {
  input: ["color"]
};

//#abc and #abcdef   but not #abcd

var result = validation.validateView("#abc", rules.input);
expect(result[0].success).to.equal(true);

Sample of custom validation

You can define "one" custome validation rule :

const validatePassword = function () {
        if (vm.user.password === vm.user.passwordConfirm) {
            return { success: true, message: '' };
        }
        return { success: false, message: 'Les deux mot de passe doivent être identique.' };
    };
    const customPassword = {
        custom: {
            message: 'a default message'
            validateView: validatePassword,
            validateModel: validatePassword
        }
    };

    vm.rules = {
        password: login.rules.password,
        passwordConfirm: ['required', customPassword],
    };

Your return message can be dynamically generated like bellow :

const getSourceRules = function(synonyme) {
  const validate = function(value) {
    const val = validateSupplierReference(value);
    if (!val.success) {
      return val;
    }

    for (let index = 0; index < vm.Synonymes.length; index++) {
      const element = vm.Synonymes[index];
      if (element.Source != null && value != null) {
        if (
          element !== synonyme &&
          element.Source.toLowerCase() === value.toLowerCase()
        ) {
          return {
            message: "Un élément ne peut pas être en doublons en source.",
            success: false
          };
        }
      }
    }

    if (
      value &&
      vm.Model.Dest &&
      value.toLowerCase() === vm.Model.Dest.toLowerCase()
    ) {
      return {
        message: "La source ne peut être égale à la cible.",
        success: false
      };
    }
    return { message: "", success: true };
  };

  const customDest = {
    custom: {
      validateView: validate,
      validateModel: validate
    }
  };

  const rules = {
    Dest: ["required", { maxLength: 40 }, customDest]
  };

  return rules.Dest;
};

Roadmap

  • Update documentation => Describe all rules, more internal detail
  • Plug moment or something else to internationalize date validation (date lib should be injected and not required)
  • Make API more fluent