验证 Javascript 中的数据的简单方式 Superstruct
- 授权协议: MIT
- 开发语言: JavaScript
- 操作系统: 跨平台
- 软件首页: https://github.com/ianstormtaylor/superstruct
- 软件文档: https://github.com/ianstormtaylor/superstruct
- 官方下载: https://github.com/ianstormtaylor/superstruct
软件介绍
Superstruct 是一个简单和可组合的方式来验证 Javascript 中的数据。它的类型注释 API 受 Typescript、Flow、Go 和 GraphQL 的启发,令用户有熟悉感且易于理解。
不过,Superstruct 是为在运行时验证数据而设计的,所以它会为最终用户抛出(或返回)详细的运行时错误。 这在类似于接受 REST 或 GraphQL API 中的任意输入的情况下特别有用。它甚至可以用来在运行时验证内部数据结构。
Usage
Superstruct 导出一个struct 工厂模式,以根据特定模式验证数据的结构:
import { struct } from 'superstruct'
const Article = struct({
id: 'number',
title: 'string',
is_published: 'boolean?',
tags: ['string'],
author: {
id: 'number',
}
})
const data = {
id: 34,
title: 'Hello World',
tags: ['news', 'features'],
author: {
id: 1,
}
}
const article = Article(data)
// This will throw when the data is invalid, and return the data otherwise.
// If you'd rather not throw, use `Struct.validate()` or `Struct.test()`.它可以识别所有原生的 JavaScript 类型。 你也可以使用 superstruct export 来定义自己的自定义数据类型(根据应用需求)
import { superstruct } from 'superstruct'
import isUuid from 'is-uuid'
import isEmail from 'is-email'
const struct = superstruct({
types: {
uuid: value => isUuid.v4(value),
email: value => isEmail(value) && value.length < 256,
}
})
const User = struct({
id: 'uuid',
email: 'email',
is_admin: 'boolean?',
})
const data = {
id: 'c8d63140-a1f7-45e0-bfc6-df72973fea86',
email: 'jane@example.com',
}
const user = User(data)
算法笔记上机训练实战指南
胡凡、曾磊 / 机械工业出版社 / 2016-7 / 57
《算法笔记上机训练实战指南》是《算法笔记》的配套习题集,内容按照《算法笔记》的章节顺序进行编排,其中整理归类了PAT甲级、乙级共150多道题的详细题解,大部分题解均编有题意、样例解释、思路、注意点、参考代码,且代码中包含了详细的注释。读者可以通过本书对《算法笔记》的知识点进行更深入的学习和理解。书中印有大量二维码,用以实时更新或补充书籍的内容及发布本书的勘误。 《算法笔记上机训练实战指南》可......一起来看看 《算法笔记上机训练实战指南》 这本书的介绍吧!
