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

simple-validators

v1.1.0

Published

Schema validation functions and error reporting framework

Downloads

4,032

Readme

simple-validators

Schema validation functions and error reporting framework

Scope

This package provides basic validation functions for building more complex schema validation. It is explicit and verbose with no magic to infer anything from existing types. Rather than throwing validation errors, these functions pass error/warning messages out as options and return undefined; the consumer may then choose how to handle error reporting.

Usage

import fs from 'node:fs';
import {
  defined,
  incrementOptions,
  validateBoolean,
  validateEmail,
  validateObjectKeys,
  validateString,
  validationError,
  ValidationOptions,
} from 'simple-validators';

// Define typescript type
export type Author = {
  id: string;
  name?: string;
  email?: string;
  corresponding?: boolean;
};

// Define validation function for object, which explicitly checks each property
export function validateAuthor(input: any, opts: ValidationOptions) {
  const value = validateObjectKeys(
    input,
    { required: ['id'], optional: ['name', 'corresponding', 'email'] },
    opts,
  );
  if (value === undefined) return undefined;
  const id = validateString(value.id, {
    ...incrementOptions('id', opts),
    regex: '^[a-z][a-zA-Z0-9]{19}$',
  });
  if (id === undefined) return undefined;
  const output: Author = { id };
  if (defined(value.name)) {
    output.name = validateString(value.name, incrementOptions('name', opts));
  }
  if (defined(value.email)) {
    output.email = validateEmail(value.email, incrementOptions('email', opts));
  }
  if (defined(value.corresponding)) {
    const correspondingOpts = incrementOptions('corresponding', opts);
    if (value.corresponding && !defined(value.email)) {
      validationError('corresponding author must have email', correspondingOpts);
    }
    output.corresponding = validateBoolean(value.corresponding, correspondingOpts);
  }
  return output;
}

// Consume validation function with logging and error handling
export function loadAuthorFromFile(authorFile: string) {
  const rawAuthor = JSON.parse(fs.readFileSync(authorFile).toString());
  const opts: ValidationOptions = {
    file: authorFile,
    property: 'author',
    messages: {},
    errorLogFn: (message: string) => console.log(`Error: ${message}`),
    warningLogFn: (message: string) => console.log(`Warning: ${message}`),
  };
  const author = validateAuthor(rawAuthor, opts);
  if (!author || opts.messages.errors?.length) {
    throw new Error(`Unable to load author from file ${authorFile}`);
  }
  return author;
}