axetroy / Struct
Licence: apache-2.0
A Modern, Scalable , Graceful, Easy Use data structure validator
Stars: ✭ 20
Programming Languages
javascript
184084 projects - #8 most used programming language
Projects that are alternatives of or similar to Struct
Tensorbase
TensorBase BE is building a high performance, cloud neutral bigdata warehouse for SMEs fully in Rust.
Stars: ✭ 440 (+2100%)
Mutual labels: data, modern
Cti Stix Validator
OASIS TC Open Repository: Validator for STIX 2.0 JSON normative requirements and best practices
Stars: ✭ 24 (+20%)
Mutual labels: validator
Dendro
"Open-source Dropbox" with added description features. It is a data storage and description platform designed to help researchers and other users to describe their data files, built on Linked Open Data and ontologies. Users can use Dendro to publish data to CKAN, Zenodo, DSpace or EUDAT's B2Share and others.
Stars: ✭ 25 (+25%)
Mutual labels: data
Pytest Patterns
A couple of examples showing how pytest and its plugins can be combined to solve real-world needs.
Stars: ✭ 24 (+20%)
Mutual labels: data
A11yc
Check accessibility of target page and generate accessibility evaluate page and policy.
Stars: ✭ 13 (-35%)
Mutual labels: validator
Graph
Graph is a semantic database that is used to create data-driven applications.
Stars: ✭ 855 (+4175%)
Mutual labels: data
Ismailfine
A simple (but correct) library for validating email addresses. Supports mail addresses as defined in rfc5322 as well as the new Internationalized Mail Address standards (rfc653x). Based on https://github.com/jstedfast/EmailValidation
Stars: ✭ 9 (-55%)
Mutual labels: validator
Modelassistant
Elegant library to manage the interactions between view and model in Swift
Stars: ✭ 26 (+30%)
Mutual labels: data
Grpc Dotnet Validator
Simple request message validator for grpc.aspnet
Stars: ✭ 25 (+25%)
Mutual labels: validator
Nada
National Data Archive (NADA) is an open source data cataloging system that serves as a portal for researchers to browse, search, compare, apply for access, and download relevant census or survey information. It was originally developed to support the establishment of national survey data archives.
Stars: ✭ 14 (-30%)
Mutual labels: data
Samples Viewer Generator
🎉 A CLI utility tool to generate web app of data visualization samples for presentation purpose
Stars: ✭ 13 (-35%)
Mutual labels: data
Fluidvalidator
Validating values with a fluent interfaced class
Stars: ✭ 8 (-60%)
Mutual labels: validator
Struct
A Modern, Scalable , Graceful, Easy Use data structure validator, Support browser and NodeJs
- [x] All in Javascript. No Magic string.
- [x] Strict mode, no one excess field.
- [x] Most of type validator support.
- [x] Scalable, easy to define your customize validator.
- [x] Highly customizable.
- [x] Validate with params, Support pass the argument to the validator.
- [x] Pipe line, multiple validator work together.
- [x] Support endless nest object, including Object and Array.
- [x] Clear error message.
- [x] Support nest Struct
Quick start
npm install @axetroy/struct --save
const { Struct, type } = require('@axetroy/struct');
const data = {
name: 'axetroy',
age: 18,
address: {
city: 'DC',
code: '12' // invalid city code, it should be an integer
}
};
const User = Struct({
name: type.string,
age: type.int,
address: {
city: type.string,
code: type.int
}
});
const err = User.validate(data);
console.log(err); // if all validator success, the error should be undefined
/**
{ Error
at Object.<anonymous> (/home/axetroy/gpm/github.com/axetroy/struct/src/error.js:19:23)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/axetroy/gpm/github.com/axetroy/struct/src/type.js:2:19)
at Module._compile (module.js:635:30)
validator: 'int',
path: [ 'address', 'code' ],
value: '12',
detail: 'Expected a value of type `int` for `address.code` but received `12`.',
message: 'Expected a value of type `int` for `address.code` but received `12`.' }
*/
Advanced usage
const { Struct, type } = require('@axetroy/struct');
const data = {
name: 'axetroy',
age: 18,
address: {
city: 'DC',
code: 100
},
message: [
{ from: 'marry', msg: 'How are you?', timestamp: 1513155028 },
{ from: 'henry', msg: "How's going one?", timestamp: 1513135028 }
]
};
const User = new Struct({
name: type.string,
age: type.int.gte(18), // age is int && and age >= 18
address: {
city: type.string,
code: type.int.gte(100)
},
message: [
{
from: type.string,
msg: type.string,
timestamp: type.int
}
]
});
const err = User.validate(data);
console.log(err); // undefined, because the data pass the validator
Document
class: Struct
Create a struct
const { Struct, type } = require('@axetroy/struct');
const struct1 = new Struct(type.string);
const struct2 = Struct(type.string);
Struct.define
staticStruct.Type
staticstruct.validate(data)
-
data
: <*> - returns: <Undefined | TypeError>
const err = Struct.validate({ word: 'Hello world' });
validate the data is match with struct, if all match. return undefined
, if not, return an TypeError
class: Type
Create a type
static: Type.define(validatorName, handler)
- validatorName:
- handler: <(input):bool | (argv):(input):bool>
Type.define('email', function(input) {
// here to check is it a email string
return true;
});
define a customize type, will add an property on type.prototype
type.xxx
const stringType = type.string;
const intType = type.int;
const composingType = type.int.gte(100);
Validator | Description | Require Argument | Source Code |
---|---|---|---|
number | Check the type is a number | false | src/validator/number |
int | Check the type is a int | false | src/validator/int |
float | Check the type is a float | false | src/validator/float |
string | Check the type is a string | false | src/validator/string |
bool | Check the type is a bool | false | src/validator/bool |
any | Any type | false | src/validator/any |
odd | Check the type is a number and odd | false | src/validator/odd |
even | Check the type is a number and even | false | src/validator/even |
json | Check the type is json string | false | src/validator/json |
eq(value) | Equal to some value | true | src/validator/eq |
gt(number) | Greater then a number | true | src/validator/gt |
gte(number) | Greater then or equal a number | true | src/validator/gte |
lt(number) | Less then a number | true | src/validator/lt |
lte(number) | Less then or equal a number | true | src/validator/lte |
bt(min, max) | Between the min and max | true | src/validator/bt |
in(array) | The value is in the array | true | src/validator/in |
len(int) | The values's length property equal to xxx | true | src/validator/len |
msg(message) | Custom error message of this field | true | src/validator/msg |
func(validatorFunc) | Custom Validator | true | src/validator/func |
All the validator is define on type.prototype
.
class: TypeError
- validator: What validator fail
- path: What key not pass the validator
- value: The value which not pass the validator
- message: The error message
- detail: The error message
The TypeError inherit from Error
Examples
- Basic Validation
- Custom Validator
- Custom Validator With Argument
- Custom Validator Function
- Custom Validator Function With Type
- Composing Validator
- Composing Struct
- Composing Struct In Array
- Custom Error Message
- Deep Nest Object
- Deep Nest Array
There is the examples, may be it can help you
Contributing
Contributors
Axetroy 💻 🐛 🎨 |
---|
License
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].