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

yup-schemas

v2.0.3

Published

Pre-build express middlewares and yup schemas to validate and parse request.

Downloads

6

Readme

yup schemas for express, mongoose

This package for project using: express, mongoose and yup.

How to install

$ npm install yup-schemas

What can yup schemas do?

For example, with reqQuery, you can easily implement features such as:

  • params valdiation
  • pagination
  • limit fields
  • sorting
  • filtering

Introduction

This package exports 2 middlewares and 4 pre-build schemas:

const {
  validateRequest, // middleware
  requiredFields, // middleware
  //
  reqParams, // yup schema to validate req.params
  reqQuery, // yup schema to parse req.query
  objectId, // use to check if string is valid mongodb objectid
  objectIdArray, // use to validate if an array only contains objectid
} = require('yups-schema');

Detail

  • validateRequest: accept yup schema as input and validate req.body. Return 400 error if validation failed.

  • requiredFields: accepts a string or multiple strings and check if they are defined. Return 400 error if the required fields are not defined.

You can use these pre-build yup schemas to:

  • reqParams: check if req.params.id is a valid objectId
  • reqQuery: validate and parse req.query
  • objectId: check if a field is a valid objectId
  • objectIdArray: check if a field is array of objectIds

How to use ?

To use yup-schemas, you first need to install yup:

$ npm install yup

Then, you need to define your schema using yup and yup-schemas

// using schema to quickly validate
const yup = require('yup');
const { objectId, reqQuery } = require('yup-schemas');

const productSchema = yup.object({
  body: yup.object({
    // write your code to validate req.body here
  }),

  // validate req.params
  params: reqParams, // check if req.params.id is a valid mongodb ObjectId

  // validate and parse req.query
  query: reqQuery,
});

Finally, import schema and pass it to route handler like this example:

// Using middleware to validate
const express = require('express');
const { requiredFields, validateRequest } = require('yup-schemas');
const router = express();

router.use(
  '/',
  requiredField('name', 'price'), // make sure name and price are required
  validateRequest(productSchema), // validate req.body, req.params and req.query using defined schema
  yourHandlerHere,
);

Here are some examples how reqQuery parse your query

There are 2 types of queries: special queries & normal queries

How special queries being parsed:

Special queries includes: page, itemsPerPage, sort, fields, skip, limit

| endpoint | req.query | | ---------------------------------- | --------------------------------------------- | | /products?page=1 | { page : 1, filter : {} } | | /products?itemsPerPage=1 | { itemsPerPage : 1, filter : {} } | | /products?fields=price,name,status | { fields:"price name status", filter : {} } | | /products?sort=price,-name,status | { sort:"price -name status", filter : {} } | | /products?skip=1 | { skip : 1, filter : {} } | | /products?limit=1 | { limit : 1, filter : {} } |

How other queries being parsed:

| endpoint | req.query | | ----------------------------------------- | ----------------------------------------------------------------- | | /products?price=24 | { filter : { price: '24' } } | | /products?price=26,12 | { filter : { price: { $in: [ '26', '12' ] } } } | | /products?price=15&status=active&price=14 | { filter : { price: { $in: [ '15', '14' ] }, status:"active"} } | | /products?price[gt]=4 | { filter : { price: { $gt: 4 }} } | | /products?price[gte]=4 | { filter : { price: { $gte: 4 }} } | | /products?price[lt]=4 | { filter : { price: { $lt: 4 }} } | | /products?price[lte]=4 | { filter : { price: { $lte: 4 }} } |

Example

Implement pagination, limit fields, sorting and filter using reqQuery.

// productSchema.js
const reqQuery = require('yup-schemas');
const yup = require('yup');

const productSchema = yup.object({
  body: yup.object({
    // your product schema definition here
  }),
  query: reqQuery,
});
// productRoutes.js
const express = require('express');
const router = express.Router();
const productSchema = require('./productSchema'); // import yup schema

// parse query and validate query before getAllProducts handler
router.get('/', validateRequest(productSchema), getAllProducts);
// productControllers.js
const express = require('express');
const router = express.Router();
const productSchema = require('./productSchema'); // import yup schema
const Product = require('./productModel'); // import your mongoose model here

const getManyProducts = async (req, res, next) => {
  const {
    fields,
    sort = '-createdAt', // new to old
    page = 1,
    itemsPerPage = 20,
    filter,
  } = req.query;

  // 0. check how many result
  const matchingResults = await Product.countDocuments(filter);
  const totalPages = Math.ceil(matchingResults / itemsPerPage);

  if (page > totalPages) {
    return res.status(200).json({
      status: 'success',
      results: 0,
      data: [],
    });
  }

  // 1. create inital query but not await it
  let query = Product.find(filter);

  // 2. sorting
  query = query.sort(sort);

  // 3. limit fields
  if (fields) {
    query = query.select(fields);
  }

  // 4. pagination
  const skip = (page - 1) * itemsPerPage;
  const limit = itemsPerPage;

  query = query.skip(skip).limit(limit);

  // 5. finally await query
  const ones = await query;

  res
    .status(200)
    .json({ status: 'success', totalPages, results: ones.length, data: ones });
};

Dependencies

This package only have 2 dependencies: yup & mongoose