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 🙏

© 2026 – Pkg Stats / Ryan Hefner

navy-schema

v1.1.5

Published

A simple data validator for JavaScript

Readme


A simple data validator for JavaScript

Installation

  • npm:npm install navy-schema
  • yarn:yarn add navy-schema

Example

import { Navy } from 'navy-schema'

const schema = Navy.object()
  .keys({
    username: Navy.string()
      .alphanum()
      .min(6)
      .max(18),
    password: Navy.string()
      .regexp(/^[a-z0-9_]{8, 20}$/i),
    repeat_password: Navy.ref('password'),

    access_token: Navy.array()
      .items([
        Navy.string().required(),
        Navy.number().required()
      ])
  })
  .optional({
    birthday: Navy.date().required(),
    email: Navy.string().mail()
    })

const data = {
  username: 'username001',
  password: 'password',
  repeat_password: 'password',
    
  access_token: ['access_token', 1024, 2048]
}
const another = Object.assign({
  birthday: '2020-02-02',
  email: '[email protected]'
}, data)

// -> true
schema.validate(data)

// -> true
schema.validate(another)

// -> false
schema.validate({})

API

schema.validate(data)

对指定数据进行校验,并返回一个 Promise 对象

schema.validateSync(data)

对指定数据进行校验,并返回布尔值

schema.validatev1.1.0 后的版本移除,schema.validateSync 更名为 schema.validate

schema.effect(status, names, hook)

Navy 添加钩子,在检验后调用;替换原本的 schema.validate 函数

  • status - 检验结果,值为 passed | failed
  • names - 需要添加钩子的规则名,值为 string 类型或包含 string 类型的数组
  • hook - 钩子函数,会按顺序传入规则函数的参数 refschema.validate 函数的参数 val
// Example
import { Navy } from 'navy-schema'

let i = 0, j = 0, k = 0
const schema = Navy.object()
  .keys({
    username: Navy.string()
      .alphanum()
        .effect('passed', 'alphanum', () => i = 1),
    password: Navy.string()
      .regexp(/^[a-z0-9_]{8, 20}$/i)
        .effect('passed', 'regexp', () => j = 1),
    repeat_password: Navy.ref('password'),
  })
  .effect('passed', 'keys', () => k = 1)

const data = {
  username: 'username001',
  password: 'password',
  repeat_password: 'password',
}

// -> true
schema.validate(data)

// -> 1, 1, 1
console.log(i, j, k)

any.required

指定该值任意类型且不为空

any.empty

指定该值为 undefinednull

any.equal(ref)

指定该值与参照对象完全一致

  • ref - any,参照对象

any.truthy

指定该值必须为 val == true

any.falsy

指定该值必须为 val == false

number.required

指定该值为 number 类型且不为空

number.greater(ref)

指定该值为 number 类型且大于参照对象

  • ref - number,参照对象

number.less(ref)

指定该值为 number 类型且小于参照对象

  • ref - number,参照对象

number.max(ref)

指定该值为 number 类型且小于等于参照对象

  • ref - number,参照对象

number.min(ref)

指定该值为 number 类型且大于等于参照对象

  • ref - number,参照对象

number.equal(ref)

指定该值为 number 类型且等于参照对象

  • ref - number,参照对象

number.int

指定该值为 number 类型且为整数

number.multiple(ref)

指定该值为 number 类型且为参照对象的倍数

  • ref - number,参照对象

number.divisor(ref)

指定该值为 number 类型且为参照对象的因数

  • ref - number,参照对象

number.positive

指定该值为 number 类型且为正数

number.negative

指定该值为 number 类型且为负数

string.required

指定该值为 string 类型且不为空

string.regexp(ref)

指定该值为 string 类型且符合正则表达式

  • ref - RegExp,正则表达式

string.includes(ref)

指定该值为 string 类型且包含参照对象

  • ref - string,参照对象

string.equal(ref)

指定该值为 string 类型且与参照对象相等

  • ref - string,参照对象

string.max(ref)

指定该值为 string 类型且字符串长度小于等于参照对象

  • ref - number,参照对象

string.min(ref)

指定该值为 string 类型且字符串长度大于等于参照对象

  • ref - number,参照对象

string.length(ref)

指定该值为 string 类型且字符串长度等于参照对象

  • ref - number,参照对象

string.number

指定该值为 string 类型且只有数字组成

string.alphabet

指定该值为 string 类型且只有字母组成

string.alphanum

指定该值为 string 类型且只有字母和数字组成

string.URL

指定该值为 string 类型且为合法 URL 地址

string.mail

指定该值为 string 类型且为合法电子邮箱

string.phone

指定该值为 string 类型且符合中华人民共和国手机号段规则

string.IDCard

指定该值为 string 类型且符合中华人民共和国身份证号码验证规则

date.required

指定该值为 ISOStringnumberDate 类型且不为空

date.after(ref)

指定该值为 ISOStringnumberDate 类型且日期在参照对象之后

  • ref - ISOStringnumberDate ,参照对象

date.before(ref)

指定该值为 ISOStringnumberDate 类型且日期在参照对象之前

  • ref - ISOStringnumberDate ,参照对象

date.at(ref)

指定该值为 ISOStringnumberDate 类型且日期等于参照对象

  • ref - ISOStringnumberDate ,参照对象

date.max(ref)

指定该值为 ISOStringnumberDate 类型且日期在参照对象之前(包含参照对象)

  • ref - ISOStringnumberDate ,参照对象

date.min(ref)

指定该值为 ISOStringnumberDate 类型且日期在参照对象之后(包含参照对象)

  • ref - ISOStringnumberDate ,参照对象

array.required

指定该值为 array 类型且不为空

array.max(ref)

指定该值为 array 类型且数组长度小于等于参照对象

  • ref - number ,参照对象

array.min(ref)

指定该值为 array 类型且数组长度大于等于参照对象

  • ref - number ,参照对象

array.length(ref)

指定该值为 array 类型且数组长度等于参照对象

  • ref - number ,参照对象

array.items(ref)

指定该值为 array 类型且数组内的值符合参照数组中的任意规则

  • ref - schema[] ,值为 Navy 实例的数组

array.only(ref)

指定该值为 array 类型且数组内的值符合参照对象规则

  • ref - schemaNavy 实例

object.required

指定该值为 object 类型且不为空

object.keys(ref)

指定该值为 object 类型且键值符合参照对象规则

  • ref - object<string, schema> ,参照对象
/** keys 和 optional 方法会根据调用顺序覆盖冲突的键值 */

object.optional(ref)

指定该值为 object 类型,该值可以为未定义或符合对象规则

  • ref - object<string, schema> ,参照对象
/** keys 和 optional 方法会根据调用顺序覆盖冲突的键值 */

ref(key, ancestor)

创建一个值的引用对象,其他 Navy 实例可以使用引用对象作为参照对象。这个方法只能在 schema<object> 中使用

  • key - string,需要引用的键名
  • ancestor - number,向父对象偏移的次数,每有一代 object 则偏移量加一
// Example
import { Navy } from 'navy-schema'

const schema = Navy.object()
  .keys({
    a: Navy.any().equal('luke'),
    b: Navy.any().equal(Navy.ref('a')),
    c: Navy.ref('a'),
    d: {
      e: Navy.ref('a', 1)
    }
  })

// -> true
schema.validate({ a: 'luke', b: 'luke', c: 'luke', d: { e: 'luke' } })

License

MIT