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

jsonval

v4.2.2

Published

一个json值验证工具

Downloads

11

Readme

JSONVAL

一个json值验证工具

使用校验

通过jsonv提供的方法即可对值进行校验

import { jsonv } from 'jsonval'

const validator = jsonv.object('!', '用户信息', {
	id: jsonv.int('!', 'ID', { range: [1, null] }),
	name: jsonv.object('!', '姓名', {
		first: jsonv.string('!', '姓氏', { length: [1, 3] }),
		last: jsonv.string('!', '名字', { length: [1, 3] }),
	}),
	gender: jsonv.string('?', '性别', { values: ['男', '女'] }),
	love: jsonv.array('?', '爱好列表', jsonv.string('!', '爱好', { length: [1,5] }))
})

const result = validator({
	id: 2,
	name: { first:'张', last:'飞' },
	gender: '男'
})

console.log(result)

扩展校验规则

通过tools.regist方法即可注册自己的校验规则(如果是typescript,则还需要扩展IJSONV接口,具体操作不做描述)

校验方式扩展示例:

import {tools} from 'jsonval'

//新扩展校验
tools.regist('id', (req, name) => {
	//只需要返回一个校验函数即可
	//校验函数的参数是需要校验的值,返回值是经过校验之后应该得到的值
	return (val)=>{
		//是否必填检测
		if (tools.empty(val)) {
			if (req == '!') tools.error(name, '不能为空')
		}
		//结果处理
		else {
			val = val + ''
			if(!/^\d{1,20}$/.test(val)) tools.error(name, '不是有效ID')
		}
		return val
	}
})

//使用现有扩展进行组合
tools.regist('userName', (req, name)=>{
	return jsonv.object(req, name, {
		first: jsonv.string(req, '姓氏', { length: [1, 3] }),
		last: jsonv.string(req, '名字', { length: [1, 3] }),
	})
})

对于扩展后的校验器的使用:

import { jsonv } from 'jsonval'

const validator = jsonv.object('!', '用户信息', {
	id: jsonv.id('!', 'ID'),
	name: jsonv.userName('!', '姓名'),
	gender: jsonv.string('?', '性别', { values: ['男', '女'] }),
	love: jsonv.array('?', '爱好列表', jsonv.string('!', '爱好', { length: [1,5] }))
})

自定义错误处理

通过自定义错误处理,可以实现国际化等其他功能。

  • 通过jsonv.tools.erraction用来注册或重写错误处理程序。
  • 通过jsonv.tools.erractionof来获取已注册的错误处理程序。
  • 先在jsonvType.IJSONVERR中定义相关的错误处理程序,之后便可以使用jsonv.tools.erraction来注册。
  • 如果已经注册了错误处理程序,则不应该直接抛出异常,而是调用错误处理程序来处理。
  • 错误处理程序参数option.voption是在进行校验时通过第二个参数传递的。