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

@knowark/validarkjs

v0.9.9

Published

Simple Data Validation Library

Downloads

284

Readme

Validarkjs

Simple Data Validation Library

Usage

Call the validate method with the required schema and the records to be validated:

import { validate } from validark

const schema = {
    "*name": String,
    "age": parseInt
}

const records = [{
    "name": "Pepito Pérez",
    "age": 64
}]

const [result] = validate(schema, records)

console.assert(result === value)

Schemas are just objects whose keys are strings and whose records are validation callables, objects or arrays. e.g.:

const schema = {
    "color": String,
    "width": parseInt,
    "height": parseInt,
    "weight": parseFloat,
    "duration": (v) => (v >= 0 && v <= 59) && v || 0
    "contact": {
        "phone": String,
        "email": (v) => v.contains('@') && v || ''
    }
}

Validation callables must receive their keys' corresponding input value and return the final value that will be assigned to such key. If an Error is received, it will be thrown:

const schema = {
    "name": String,
    "age": (v) => (v > 0 && v < 130) && v || new Error("Invalid Age!")
}

const message = None

try {
    const records = [{"name": "John Doe", "age": 200}]
    const [result] = validate(schema, records)
} catch (error) {
    const message = String(error)
}

console.assert(message === "Error: Invalid Age!")

Mandatory fields can be marked with an asterix (*) as key prefix:

const schema = {
    "title": String,
    "*firstname": String,
    "*surname": String,
}

Aliases can be delimited with :=. The final key will be the leftmost entry:

const schema = {
    "*first_name:=firstname:=firstName": String,
    "*last_name:=lastname:=lastName": String
}

const records = [
    {"firstName": "Clark", "lastName": "Kent"},
    {"firstname": "Peter", "lastname": "Parker"},
    {"first_name": "Bruce", "last_name": "Wayne"}
]

const result = validate(schema, records)

console.assert(result === [
    {"first_name": "Clark", "last_name": "Kent"},
    {"first_name": "Peter", "last_name": "Parker"},
    {"first_name": "Bruce", "last_name": "Wayne"}
])

Extra keys in the records' entries are ignored and aliases definitions are processed from right to left if there are multiple matches:

const schema = {
    "*name": String,
    "*player_id:=playerId": String,
    "*score:=totalScore:=points": parseInt
}

const records = [
    {"name": "James", "playerId": "007", "totalScore": 99, "points": 55}
]

const [result] = validate(schema, records)

console.assert(result === {
    "name": "James", "player_id": "007", "score": 99
})

Sequences of items might be handled by defining the validation function inside an array:

const schema = {
    "levels": [String],
    "addresses": [
        {'*street': String, 'city': String}
    ]
}

const records = [{
    "levels": [1, 2, 3],
    "addresses": [
        {"street": '5th Ave 45', "city": "Popeland"},
        {"street": '7th Street 67', "city": "Churchland"}
    ]
}]

const [result] = validate(schema, records)

console.assert(result === {
    "levels": ["1", "2", "3"],
    "addresses": [
        {"street": '5th Ave 45', "city": "Popeland"},
        {"street": '7th Street 67', "city": "Churchland"}
    ]
})

Utilities

Validark includes several utility functions to simplify common validation scenarios.

check

The check() function behaves similarly to the assert() function in NodeJS. However, this function is provided as a convenient multi-environment utility. check() returns the provided value if it is truthy.

const value = 'Truthy Value'

const result = check(value)

const [result] = validate(schema, records)

console.assert(result === value)

In the other hand, if the value provided to check is falsy, it raises an error with the optional provided message.

const value = undefined

try {
  check(value, 'Invalid Value!')
} catch (error) {
  const message = String(error)
}

console.assert(message === "check failed. Invalid Value!")

Or called without arguments:

try {
  check()
} catch (error) {
  const message = String(error)
}

console.assert(message === "check failed.")

grab

The grab() retrieves a key from an object.

const object = {
  element: 'content'
}

const result = grab(object, 'element')

console.assert(result === "content")

It errors out if the key is not found in the object.

const object = {
  element: 'content'
}

try {
  grab(object, 'missing')
} catch (error) {
  const message = String(error)
}

console.assert(message === 'Key "missing" not found')

It can also provide a fallback value in case the key is not found in the container object.

const object = {
  element: 'content'
}

const value =  grab(object, 'missing', 777)

console.assert(value === 777)

The grab() function can also receive a Class as its key argument. In that case, an instance of such class will be tried to be obtained from the provided object container. The name of the key wold be either the name of the class in camelCase or just its unaltered string name.

class Alpha {}

const object = {
  alpha: new Alpha()
}

const value = grab(object, Alpha)

console.assert(value instanceof Alpha)