Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
schema-typed
Advanced tools
Schema for data modeling & validation
English | 中文版
npm install schema-typed --save
import { SchemaModel, StringType, DateType, NumberType } from 'schema-typed';
const model = SchemaModel({
username: StringType().isRequired('Username required'),
email: StringType().isEmail('Email required'),
age: NumberType('Age should be a number').range(
18,
30,
'Age should be between 18 and 30 years old'
)
});
const checkResult = model.check({
username: 'foobar',
email: 'foo@bar.com',
age: 40
});
console.log(checkResult);
checkResult
return structure is:
{
username: { hasError: false },
email: { hasError: false },
age: { hasError: true, errorMessage: 'Age should be between 18 and 30 years old' }
}
StringType()
.minLength(6, "Can't be less than 6 characters")
.maxLength(30, 'Cannot be greater than 30 characters')
.isRequired('This field required');
Customize a rule with the addRule
function.
If you are validating a string type of data, you can set a regular expression for custom validation by the pattern
method.
const model = SchemaModel({
field1: StringType().addRule((value, data) => {
return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, 'Please enter legal characters'),
field2: StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, 'Please enter legal characters')
});
model.check({ field1: '', field2: '' });
/**
{
field1: {
hasError: true,
errorMessage: 'Please enter legal characters'
},
field2: {
hasError: true,
errorMessage: 'Please enter legal characters'
}
};
**/
E.g: verify that the two passwords are the same.
const model = SchemaModel({
password1: StringType().isRequired('This field required'),
password2: StringType().addRule((value, data) => {
if (value !== data.password1) {
return false;
}
return true;
}, 'The passwords are inconsistent twice')
});
model.check({ password1: '123456', password2: 'root' });
/**
{
password1: { hasError: false },
password2: {
hasError: true,
errorMessage: 'The passwords are inconsistent twice'
}
};
**/
Validate nested objects, which can be defined using the ObjectType().shape
method. E.g:
const model = SchemaModel({
id: NumberType().isRequired('This field required'),
name: StringType().isRequired('This field required'),
info: ObjectType().shape({
email: StringType().isEmail('Should be an email'),
age: NumberType().min(18, 'Age should be greater than 18 years old')
});
});
It is more recommended to flatten the object.
import { flaser } from 'object-flaser';
const model = SchemaModel({
id: NumberType().isRequired('This field required'),
name: StringType().isRequired('This field required'),
'info.email': StringType().isEmail('Should be an email'),
'info.age': NumberType().min(18, 'Age should be greater than 18 years old')
});
const user = flaser({
id: 1,
name: 'schema-type',
info: {
email: 'schema-type@gmail.com',
age: 17
}
});
model.check(data);
SchemaModel
provides a static method combine
that can be combined with multiple SchemaModel
to return a new SchemaModel
.
const model1 = SchemaModel({
username: StringType().isRequired('This field required'),
email: StringType().isEmail('Should be an email')
});
const model2 = SchemaModel({
username: StringType().minLength(7, "Can't be less than 7 characters"),
age: NumberType().range(18, 30, 'Age should be greater than 18 years old')
});
const model3 = SchemaModel({
groupId: NumberType().isRequired('This field required')
});
const model4 = SchemaModel.combine(model1, model2, model3);
model4.check({
username: 'foobar',
email: 'foo@bar.com',
age: 40,
groupId: 1
});
static
combine(...models)const model1 = SchemaModel({
username: StringType().isRequired('This field required')
});
const model2 = SchemaModel({
email: StringType().isEmail('Please input the correct email address')
});
const model3 = SchemaModel.combine(model1, model2);
const model = SchemaModel({
username: StringType().isRequired('This field required'),
email: StringType().isEmail('Please input the correct email address')
});
model.check({
username: 'root',
email: 'root@email.com'
});
const model = SchemaModel({
username: StringType().isRequired('This field required'),
email: StringType().isEmail('Please input the correct email address')
});
model.checkForField('username', 'root');
StringType().isRequired('This field required');
StringType().isEmail('Please input the correct email address');
StringType().isURL('Please enter the correct URL address');
StringType().isOneOf(['Javascript', 'CSS'], 'Can only type `Javascript` and `CSS`');
StringType().containsLetter('Must contain English characters');
StringType().containsUppercaseLetter('Must contain uppercase English characters');
StringType().containsLowercaseLetter('Must contain lowercase English characters');
StringType().containsLetterOnly('English characters that can only be included');
StringType().containsNumber('Must contain numbers');
StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, 'Please enter legal characters');
StringType().rangeLength(6, 30, 'The number of characters can only be between 6 and 30');
StringType().minLength(6, 'Minimum 6 characters required');
StringType().maxLength(30, 'The maximum is only 30 characters.');
StringType().addRule((value, data) => {
return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, 'Please enter a legal character.');
NumberType().isRequired('This field required');
NumberType().isInteger('It can only be an integer');
NumberType().isOneOf([5, 10, 15], 'Can only be `5`, `10`, `15`');
NumberType().pattern(/^[1-9][0-9]{3}$/, 'Please enter a legal character.');
NumberType().range(18, 40, 'Please enter a number between 18 - 40');
NumberType().min(18, 'Minimum 18');
NumberType().max(40, 'Maximum 40');
NumberType().addRule((value, data) => {
return value % 5 === 0;
}, 'Please enter a valid number');
ArrayType().isRequired('This field required');
ArrayType().rangeLength(1, 3, 'Choose at least one, but no more than three');
ArrayType().minLength(1, 'Choose at least one');
ArrayType().maxLength(3, "Can't exceed three");
ArrayType().unrepeatable('Duplicate options cannot appear');
ArrayType().of(StringType().isEmail(), 'wrong format');
ArrayType().addRule((value, data) => {
return value.length % 2 === 0;
}, 'Good things are in pairs');
DateType().isRequired('This field required');
DateType().range(
new Date('08/01/2017'),
new Date('08/30/2017'),
'Date should be between 08/01/2017 - 08/30/2017'
);
DateType().min(new Date('08/01/2017'), 'Minimum date 08/01/2017');
DateType().max(new Date('08/30/2017'), 'Maximum date 08/30/2017');
DateType().addRule((value, data) => {
return value.getDay() === 2;
}, 'Can only choose Tuesday');
ObjectType().isRequired('This field required');
ObjectType().shape({
email: StringType().isEmail('Should be an email'),
age: NumberType().min(18, 'Age should be greater than 18 years old')
});
ObjectType().addRule((value, data) => {
if (value.id || value.email) {
return true;
}
return false;
}, 'Id and email must have one that cannot be empty');
BooleanType().isRequired('This field required');
ObjectType().addRule((value, data) => {
if (typeof value === 'undefined' && A === 10) {
return false;
}
return true;
}, 'This value is required when A is equal to 10');
Default check priority:
If the third argument to addRule is true
, the priority of the check is as follows:
FAQs
Schema for data modeling & validation
We found that schema-typed demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.