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

validation-report

v1.5.0

Published

Unified validation report interface

Downloads

10

Readme

Validation Report

Unified validation report interface. It specifies interface for the validation issues reporting and issues interface. This "low level" library is for creating validators also it could be used with manual validation.

Build


Installation

Install via npm:

npm i validation-report

Usage

Validation report is a collection of objects which contains validation result data (issues). Each issue has path, rule and details properties.

Example of issues:

{
    path: ['images', 0, 'title'],
    rule: 'required',
    details: {},
}

{
    path: ['user', 'age'],
    rule: 'min',
    details: {should: 18, is: 16},
}

{
    path: ['file.js'],
    rule: 'syntax',
    details: {line: 0, pos: 0, lang: 'javascript', is: '%'},
}

Such objects are very simple to send, process or stringify.

Path: string[]

Path is array to allow any characters (including dots, slashes or other separators) as property name.

Rule: string

Validation rule code unique across your application.

Details: object

Custom data object representing error data.

Example

Let create simple one function number validator:

const Report = require('validation-report');

function checkNumber(value) {
    const report = new Report();

    if (typeof value !== 'number') {
        report.addIssue({
            rule: 'type',
            details: {
                should: 'number',
                is: typeof value,
            },
        });
    }

    return report;
}

const report = checkNumber(null);

if (report.hasIssues()) {
    console.log('Looks like it is not a Number');
    const issue = report.findIssue(); // => {path: [], issue: 'type', details: {should: 'number', is: 'object'}}
}

Example for Express

Create and use report without validator in express:

const Report = require('validation-report');
const express = require('express');

express()
    .use((req, res, next) => {
        let report = new Report({
            value: req.query,
        });

        if ('id' in req.query === false) {
            report.addIssue('id', 'exists');
        }
        else if (req.query.id.length != 24) {
            report.addIssue('id', 'length', {
                accept: 32,
                result: req.query.id.length,
            });
        }

        if (! report.isValid()) {
            res.status(400)
            .json({
                code: 'validation',
                error: 'Request validation error',
                details: {issues: report},
            });
        }
        else {
            // do something...
            res.end();
        }
    })

API

constructor({issues:ReportIssue[], value: * }) -> Report

Constructor accepts an object as an only argument. This object could provides validating value and array of issues.

setIssues(issues:ReportIssue[]) -> Report

Add a list of issues.

getIssues() -> ReportIssue[]

Return an array of issues.

addIssue(issue:ReportIssue) -> Report

Add an issue.

hasIssue(path:string|string[]) -> Boolean

Check is issue exists by it's path. If path is a string separate it with '.'.

findIssue(path:function(reportIssue) -> Bool|string|string[], [rule]) -> ReportIssue

Search issue with function or by path or by path and rule. Returns ReportIssue or undefined if issue not found.

isValid() -> Boolean

DEPRECATED since version 1.5.0 use Report.hasIssues.

Check if report contains no issues.

hasIssues() -> Boolean

Check if report contains has issues.

toJSON() -> ReportIssue[]

Return array of report issues.

License

MIT.