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 🙏

© 2026 – Pkg Stats / Ryan Hefner

validate-contract

v1.3.2

Published

JSON schema validator for the server and client sides.

Readme

validate-contract

JSON schema validator for the server and client sides.

1. INSTALLATION

Install the library with npm i validate-contract

2. TYPES OF USAGE

This library supports server and client side usage.

2.1. ECMAScript (ES) Module Usage

import { validate } from "validate-contract";

const register = {
  user_mail: "user@test",
  pwd: "12",
};

const contract = [
  { type: "length", name: "user_mail", options: [10, 50] },
  { type: "email", name: "user_mail" },
  { type: "password", name: "pwd", options: [3, 12] },
];

const result = validate(contract, register);
console.log(result);

console.log

[
  { message: 'Length should be min 10 and max 50', name: 'user_mail' },
  { message: 'Invalid email!', name: 'user_mail' },
  { message: 'Length should be min 3 and max 12', name: 'pwd' }
]

2.2. Server Side Usage

validate-contract is a set of express.js middlewares that wraps validator.js validator functions.

  • req.body
  • req.cookies
  • req.headers
  • req.params
  • req.query
import { middleware } from "validate-contract";
const { body, cookies, headers, params, query } = middleware;

Middleware Using Example

import express from "express";
import { middleware } from "validate-contract";

const app = express();
const { body } = middleware;

app.use(express.json());

const contract = [
  { type: "email", name: "email" },
  { type: "password", name: "password" },
];

app.use(body(contract));

app.use("/", (req, res, next) => {
  if (req.validationCheck) {
    console.log(req.validationResult);
    return res.status(400).json({ message: req.validationResult });
  }
  res.status(200).json("Ok!");
});

app.listen(3000, () => {
  console.log("Server is running!");
});

2.3. Browser Side Usage

You can also insert the CDN link in the field at the bottom for javascript.

<script src="https://unpkg.com/validate-contract@latest/validateContract.min.js"></script>

<!-- CDN -->
<script src="https://unpkg.com/validate-contract@latest/validateContract.min.js"></script>
<!-- Standalone Script -->
<script type="text/javascript" src="validateContract.min.js"></script>

<script type="text/javascript">
  const login = {
    email: "user@test",
    password: "12",
  };

  const contract = [
    { type: "email", name: "email" },
    { type: "password", name: "password" },
  ];

  const result = validateContract.validate(contract, login);
  console.log(result);
</script>

console.log

[
  { message: 'Invalid email!', name: 'email' },
  { message: 'Length should be min 4 and max 10', name: 'password' }
]

3. LIST OF THE VALIDATION TYPES

{
  type: "integer",               // mandatory field for validation type
  name: "day",                   // mandatory field for validation parameter
  options: [1, 31],              // optional field for additional validation features
  message: "Invalid Day Length!" // optional field for custom message
}

| Types of Validation | Description | | :------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | integer | Used for integer type and lengthThe first argument of the options is for min and the second argument is for max.Default options, options: [0, 500] | | length | Used for text lengthThe first argument of the options is for min and the second argument is for max.Default options, options: [0, 500] | | email | Used for valid email type | | password | Used for complex password type and lengththe arguments are as follows => [minLength, maxLength, minLowerCase, minUpperCase, minNumber].Default options, options: [4, 10, 0, 0, 1] | | phone | Used for phone number type | | match | Checks whether it has the same value as another parameter in the same payload. | | optional | Checks whether it can take an empty value. |

Don't be surprised if other types are added soon :wink:


LICENSE

MIT Licensed.

Copyright © Hidayet AYDIN 2022.