Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
schema-typed
Advanced tools
Schema for data modeling & validation
isRequired(errorMessage?: string, trim: boolean = true)
isRequiredOrEmpty(errorMessage?: string, trim: boolean = true)
addRule(onValid: Function, errorMessage?: string, priority: boolean)
when(condition: (schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>) => Type)
check(value: ValueType, data?: DataType):CheckResult
checkAsync(value: ValueType, data?: DataType):Promise<CheckResult>
isEmail(errorMessage?: string)
isURL(errorMessage?: string)
isOneOf(items: string[], errorMessage?: string)
containsLetter(errorMessage?: string)
containsUppercaseLetter(errorMessage?: string)
containsLowercaseLetter(errorMessage?: string)
containsLetterOnly(errorMessage?: string)
containsNumber(errorMessage?: string)
pattern(regExp: RegExp, errorMessage?: string)
rangeLength(minLength: number, maxLength: number, errorMessage?: string)
minLength(minLength: number, errorMessage?: string)
maxLength(maxLength: number, errorMessage?: string)
npm install schema-typed --save
import { SchemaModel, StringType, DateType, NumberType, ObjectType, ArrayType } 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, 'Over the age limit'),
tags: ArrayType().of(StringType('The tag should be a string').isRequired()),
role: ObjectType().shape({
name: StringType().isRequired('Name required'),
permissions: ArrayType().isRequired('Permissions required')
})
});
const checkResult = model.check({
username: 'foobar',
email: 'foo@bar.com',
age: 40,
tags: ['Sports', 'Games', 10],
role: { name: 'administrator' }
});
console.log(checkResult);
checkResult
return structure is:
{
username: { hasError: false },
email: { hasError: false },
age: { hasError: true, errorMessage: 'Over the age limit' },
tags: {
hasError: true,
array: [
{ hasError: false },
{ hasError: false },
{ hasError: true, errorMessage: 'The tag should be a string' }
]
},
role: {
hasError: true,
object: {
name: { hasError: false },
permissions: { hasError: true, errorMessage: 'Permissions required' }
}
}
};
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'
}
}
**/
For example, verify that the mailbox is duplicated
function asyncCheckEmail(email) {
return new Promise(resolve => {
setTimeout(() => {
if (email === 'foo@domain.com') {
resolve(false);
} else {
resolve(true);
}
}, 500);
});
}
const model = SchemaModel({
email: StringType()
.isEmail('Please input the correct email address')
.addRule((value, data) => {
return asyncCheckEmail(value);
}, 'Email address already exists')
.isRequired('This field cannot be empty')
});
model.checkAsync({ email: 'foo@domain.com' }).then(checkResult => {
console.log(checkResult);
/**
{
email: {
hasError: true,
errorMessage: 'Email address already exists'
}
};
**/
});
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')
})
});
const user = {
id: 1,
name: '',
info: { email: 'schema-type', age: 17 }
};
model.check(data);
/**
{
"id": { "hasError": false },
"name": { "hasError": true, "errorMessage": "This field required" },
"info": {
"hasError": true,
"object": {
"email": { "hasError": true, "errorMessage": "Should be an email" },
"age": { "hasError": true, "errorMessage": "Age should be greater than 18 years old" }
}
}
}
*/
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
});
SchemaModel is a JavaScript schema builder for data model creation and validation.
static combine(...models)
A static method for merging multiple 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);
check(data: object)
Check whether the data conforms to the model shape definition. Return a check result.
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'
});
checkAsync(data: object)
Asynchronously check whether the data conforms to the model shape definition. Return a check result.
const model = SchemaModel({
username: StringType()
.isRequired('This field required')
.addRule(value => {
return new Promise(resolve => {
// Asynchronous processing logic
});
}, 'Username already exists'),
email: StringType().isEmail('Please input the correct email address')
});
model
.checkAsync({
username: 'root',
email: 'root@email.com'
})
.then(result => {
// Data verification result
});
checkForField(fieldName: string, data: object)
Check whether a field in the data conforms to the model shape definition. Return a check result.
const model = SchemaModel({
username: StringType().isRequired('This field required'),
email: StringType().isEmail('Please input the correct email address')
});
const data = {
username: 'root'
};
model.checkForField('username', data);
checkForFieldAsync(fieldName: string, data: object)
Asynchronously check whether a field in the data conforms to the model shape definition. Return a check result.
const model = SchemaModel({
username: StringType()
.isRequired('This field required')
.addRule(value => {
return new Promise(resolve => {
// Asynchronous processing logic
});
}, 'Username already exists'),
email: StringType().isEmail('Please input the correct email address')
});
const data = {
username: 'root'
};
model.checkForFieldAsync('username', data).then(result => {
// Data verification result
});
Creates a type that matches all types. All types inherit from this base type.
isRequired(errorMessage?: string, trim: boolean = true)
MixedType().isRequired('This field required');
isRequiredOrEmpty(errorMessage?: string, trim: boolean = true)
MixedType().isRequiredOrEmpty('This field required');
addRule(onValid: Function, errorMessage?: string, priority: boolean)
MixedType().addRule((value, data) => {
return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, 'Please enter a legal character.');
when(condition: (schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>) => Type)
Define data verification rules based on conditions.
const model = SchemaModel({
age: NumberType().min(18, 'error'),
contact: MixedType().when(schema => {
const checkResult = schema.age.check();
return checkResult.hasError
? StringType().isRequired('Please provide contact information')
: StringType();
})
});
/**
{
age: { hasError: false },
contact: { hasError: false }
}
*/
model.check({ age: 18, contact: '' });
/*
{
age: { hasError: true, errorMessage: 'error' },
contact: {
hasError: true,
errorMessage: 'Please provide contact information'
}
}
*/
model.check({ age: 17, contact: '' });
check(value: ValueType, data?: DataType):CheckResult
const type = MixedType().addRule(v => {
if (typeof v === 'number') {
return true;
}
return false;
}, 'Please enter a valid number');
type.check('1'); // { hasError: true, errorMessage: 'Please enter a valid number' }
type.check(1); // { hasError: false }
checkAsync(value: ValueType, data?: DataType):Promise<CheckResult>
const type = MixedType().addRule(v => {
return new Promise(resolve => {
setTimeout(() => {
if (typeof v === 'number') {
resolve(true);
} else {
resolve(false);
}
}, 500);
});
}, 'Please enter a valid number');
type.checkAsync('1').then(checkResult => {
// { hasError: true, errorMessage: 'Please enter a valid number' }
});
type.checkAsync(1).then(checkResult => {
// { hasError: false }
});
Define a string type. Supports all the same methods as MixedType.
isEmail(errorMessage?: string)
StringType().isEmail('Please input the correct email address');
isURL(errorMessage?: string)
StringType().isURL('Please enter the correct URL address');
isOneOf(items: string[], errorMessage?: string)
StringType().isOneOf(['Javascript', 'CSS'], 'Can only type `Javascript` and `CSS`');
containsLetter(errorMessage?: string)
StringType().containsLetter('Must contain English characters');
containsUppercaseLetter(errorMessage?: string)
StringType().containsUppercaseLetter('Must contain uppercase English characters');
containsLowercaseLetter(errorMessage?: string)
StringType().containsLowercaseLetter('Must contain lowercase English characters');
containsLetterOnly(errorMessage?: string)
StringType().containsLetterOnly('English characters that can only be included');
containsNumber(errorMessage?: string)
StringType().containsNumber('Must contain numbers');
pattern(regExp: RegExp, errorMessage?: string)
StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, 'Please enter legal characters');
rangeLength(minLength: number, maxLength: number, errorMessage?: string)
StringType().rangeLength(6, 30, 'The number of characters can only be between 6 and 30');
minLength(minLength: number, errorMessage?: string)
StringType().minLength(6, 'Minimum 6 characters required');
maxLength(maxLength: number, errorMessage?: string)
StringType().maxLength(30, 'The maximum is only 30 characters.');
Define a number type. Supports all the same methods as MixedType.
isInteger(errorMessage?: string)
NumberType().isInteger('It can only be an integer');
isOneOf(items: number[], errorMessage?: string)
NumberType().isOneOf([5, 10, 15], 'Can only be `5`, `10`, `15`');
pattern(regExp: RegExp, errorMessage?: string)
NumberType().pattern(/^[1-9][0-9]{3}$/, 'Please enter a legal character.');
range(minLength: number, maxLength: number, errorMessage?: string)
NumberType().range(18, 40, 'Please enter a number between 18 - 40');
min(min: number, errorMessage?: string)
NumberType().min(18, 'Minimum 18');
max(max: number, errorMessage?: string)
NumberType().max(40, 'Maximum 40');
Define a array type. Supports all the same methods as MixedType.
isRequiredOrEmpty(errorMessage?: string)
ArrayType().isRequiredOrEmpty('This field required');
rangeLength(minLength: number, maxLength: number, errorMessage?: string)
ArrayType().rangeLength(1, 3, 'Choose at least one, but no more than three');
minLength(minLength: number, errorMessage?: string)
ArrayType().minLength(1, 'Choose at least one');
maxLength(maxLength: number, errorMessage?: string)
ArrayType().maxLength(3, "Can't exceed three");
unrepeatable(errorMessage?: string)
ArrayType().unrepeatable('Duplicate options cannot appear');
of(type: object)
ArrayType().of(StringType('The tag should be a string').isRequired());
Define a date type. Supports all the same methods as MixedType.
range(min: Date, max: Date, errorMessage?: string)
DateType().range(
new Date('08/01/2017'),
new Date('08/30/2017'),
'Date should be between 08/01/2017 - 08/30/2017'
);
min(min: Date, errorMessage?: string)
DateType().min(new Date('08/01/2017'), 'Minimum date 08/01/2017');
max(max: Date, errorMessage?: string)
DateType().max(new Date('08/30/2017'), 'Maximum date 08/30/2017');
Define a object type. Supports all the same methods as MixedType.
shape(fields: object)
ObjectType().shape({
email: StringType().isEmail('Should be an email'),
age: NumberType().min(18, 'Age should be greater than 18 years old')
});
Define a boolean type. Supports all the same methods as MixedType.
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
The npm package schema-typed receives a total of 52,056 weekly downloads. As such, schema-typed popularity was classified as popular.
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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.