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

ajv-moment

v2.0.6

Published

robust date validation for ajv

Downloads

2,563

Readme

ajv-moment

an ajv plugin using moment for robust date validation in your json-schemas.

Installing

npm install --save ajv moment ajv-moment

Getting Started

import Ajv from 'ajv'
import { plugin } from 'ajv-moment'
import moment from 'moment'

// define an ajv instance
const ajv = new Ajv();

// install the plugin. by default, this plugin utilizes the "moment" custom keyword
plugin({ ajv, moment });

// define your schemas using the "moment" keyword
// here we define a schema for an object with two keys (assigned & due)
// each of which must be a valid date string in ISO 8601 format.
// in addition, we specify that the due key must be greater than or
// equal to the assigned date plus 3 days.
const schema = {
    type: 'object',
    properties: {
        assigned: {
            type: 'string',
            moment: true
        },
        due: {
            type: 'string',
            moment: {
                validate: [{
                    test: 'isSameOrAfter',
                    value: {
                        $data: '1/assigned',
                        manipulate: [{
                            add: [3, 'days']
                        }]
                    }
                }]
            }
        }
    },
    required: [
        'assigned', 'due'
    ]
}

// compile your schema using ajv
const validate = ajv.compile(schema);

validate({
    assigned: new Date().toISOString(),
    due: moment().add(24, 'hours').toISOString()
});
// false

validate({
    assigned: new Date().toISOString(),
    due: moment().add(1, 'weeks').toISOString()
});
// true

Schemas

The custom keyword schema definitions can take on many forms. The simplest being the following:

{
    "type": "string",
    "description": "a valid date string in ISO 8601 format",
    "moment": true
}

This schema will simply enforce that the provided value is a valid date string in ISO 8601 format.

To specify a custom format, a format option can be defined in the schema definition. When a custom format is specified, moment's strict parsing option will be enforced.

{
    "type": "string",
    "description": "a valid date string in MM-DD-YYYY format",
    "moment": {
        "format": ["MM-DD-YYYY"]
    }
}

Aside from date string formats, this plugin can also perform additional validations using moment. The validations are defined in the validate subschema keyword and can be used to enforce relative constraints on date strings. See below for some examples.

[
    {
        "type": "string",
        "description": "a valid date string prior to January 1st, 2000 UTC",
        "moment": {
            "validate": [
                {
                    "test": "isBefore",
                    "value": "2000-01-01T00:00:00.000Z"
                }
            ]
        }
    },
    {
        "type": "object",
        "properties": {
            "assigned": {
                "type": "string",
                "description": "a valid date string in ISO 8601 format",
                "moment": true
            },
            "due": {
                "type": "string",
                "description": "a valid date string in ISO 8601 format that is greater than or equal to the assigned date plus 3 days & 30 minutes",
                "moment": {
                    "validate": [
                        {
                            "test": "isSameOrAfter",
                            "value": {
                                "$data": "1/assigned",
                                "manipulate": [
                                    {
                                        "add": [3, "days"]
                                    },
                                    {
                                        "add": [30, "minutes"]
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "required": [
            "assigned",
            "due"
        ]
    },
    {
        "type": "object",
        "properties": {
            "first": {
                "type": "string",
                "description": "a valid date string in ISO 8601 format",
                "moment": true
            },
            "second": {
                "type": "string",
                "description": "a valid date string in ISO 8601 format that is greater than the 'first' attribute by at least 30 minutes",
                "moment": {
                    "validate": [
                        {
                            "test": "isSameOrAfter",
                            "value": {
                                "$data": "1/first",
                                "manipulate": [
                                    {
                                        "add": [30, "minutes"]
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            "breakpoint": {
                "type": "string",
                "description": "a valid date string in ISO 8601 format that is both between 'first' and 'second' and also prior to 'second' by at least 15 seconds",
                "moment": {
                    "validate": [
                        {
                            "test": "isBetween",
                            "value": [
                                {
                                    "$data": "1/first"
                                },
                                {
                                    "$data": "1/second"
                                }
                            ]
                        },
                        {
                            "test": "isSameOrBefore",
                            "value": {
                                "$data": "1/second",
                                "manipulate": [
                                    {
                                        "subtract": [15, "seconds"]
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            "third": {
                "type": "string",
                "description": "a valid date string prior in ISO 8601 format that after 'second'",
                "moment": {
                    "validate": [
                        {
                            "test": "isAfter",
                            "value": {
                                "$data": "1/second"
                            }
                        }
                    ]
                }
            }
        },
        "required": [
            "first",
            "second",
            "breakpoint",
            "third"
        ]
    }
]

Testing

run all tests

npm test

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Copyright (c) 2016 Chris Ludden. Licensed under the MIT license.