前端開發過程當中大家以爲處理什麼業務功能最煩人?前端
作前端已經有很長一段時間了,不知道你們是否和我有一樣的感覺,在一些 Web 應用中表單處理起來比其餘功能模塊都麻煩,不少體力活,每每在數據的校驗會很費時間。git
爲了可以把這部分代碼更有條理,咱們把數據校驗部分經過 Schema 預先定義一個數據模型,把數據扔進去,返回校驗結果。github
接下來我介紹一下這個工具,schema-typed 是一個數據建模及數據驗證工具, 它能夠很是方便的設計的表單數據結構,固然它不限於在表單使用。若是你在產品中使用了 React , 那配合 React Suite 的表單組件簡直就是如虎添翼。正則表達式
npm install schema-typed --save
複製代碼
import { SchemaModel, StringType, DateType, NumberType } from 'schema-typed';
const model = SchemaModel({
username: StringType().isRequired('用戶名不能爲空'),
email: StringType().isEmail('請輸入正確的郵箱'),
age: NumberType('年齡應該是一個數字').range(18, 30, '年齡應該在 18 到 30 歲之間')
});
const checkResult = model.check({
username: 'foobar',
email: 'foo@bar.com',
age: 40
});
console.log(checkResult);
複製代碼
checkResult
返回結構是:npm
{
username: { hasError: false },
email: { hasError: false },
age: { hasError: true, errorMessage: '年齡應該在 18 到 30 歲之間' }
}
複製代碼
StringType()
.minLength(6, '不能少於 6 個字符')
.maxLength(30, '不能大於 30 個字符')
.isRequired('該字段不能爲空');
複製代碼
經過 addRule
函數自定義一個規則。bash
若是是對一個字符串類型的數據進行驗證,能夠經過 pattern
方法設置一個正則表達式進行自定義驗證。數據結構
const model = SchemaModel({
field1: StringType().addRule((value, data) => {
return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, '請輸入合法字符'),
field2: StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, '請輸入合法字符')
});
model.check({ field1: '', field2: '' });
/** { field1: { hasError: true, errorMessage: '請輸入合法字符' }, field2: { hasError: true, errorMessage: '請輸入合法字符' } }; **/
複製代碼
例如,驗證兩次輸入密碼是否一致函數
const model = SchemaModel({
password1: StringType().isRequired('該字段不能爲空'),
password2: StringType().addRule((value, data) => {
if (value !== data.password1) {
return false;
}
return true;
}, '兩次密碼不一致')
});
model.check({ password1: '123456', password2: 'root' });
/** { password1: { hasError: false }, password2: { hasError: true, errorMessage: '兩次密碼不一致' } }; **/
複製代碼
對於複雜的嵌套的 Object , 可使用 ObjectType().shape 方法進行定義,好比:工具
const model = SchemaModel({
id: NumberType().isRequired('該字段不能爲空'),
name: StringType().isRequired('用戶名不能爲空'),
info: ObjectType().shape({
email: StringType().isEmail('應該是一個 email'),
age: numberType().min(18, '年齡應該大於18歲')
});
});
複製代碼
另外,更推薦把對象扁平化設計ui
import { flaser } from 'object-flaser';
const model = SchemaModel({
id: NumberType().isRequired('該字段不能爲空'),
name: StringType().isRequired('用戶名不能爲空'),
'info.email': StringType().isEmail('應該是一個 email'),
'info.age': numberType().min(18, '年齡應該大於18歲')
});
const user = flaser({
id: 1,
name: 'schema-type',
info: {
email: 'schema-type@gmail.com',
age: 17
}
});
model.check(data);
複製代碼
SchemaModel
提供了一個靜態方法 combine
, 能夠對多個 SchemaModel
合併返回一個新的 SchemaModel
。
const model1 = SchemaModel({
username: StringType().isRequired('用戶名不能爲空'),
email: StringType().isEmail('請輸入正確的郵箱')
});
const model2 = SchemaModel({
username: StringType().minLength(7, '最少7個字符'),
age: NumberType().range(18, 30, '年應該在 18 到 30 歲')
});
const model3 = SchemaModel({
groupId: NumberType().isRequired('該字段不能爲空')
});
const model4 = SchemaModel.combine(model1, model2, model3);
model4.check({
username: 'foobar',
email: 'foo@bar.com',
age: 40,
groupId: 1
});
複製代碼
const model = SchemaModel({
username: StringType().isRequired('該字段不能爲空'),
email: StringType().isEmail('請輸入正確的郵箱')
});
model.check({
username: 'root',
email: 'root@email.com'
});
複製代碼
const model = SchemaModel({
username: StringType().isRequired('該字段不能爲空'),
email: StringType().isEmail('請輸入正確的郵箱')
});
model.checkForField('username', 'root');
複製代碼
StringType().isRequired('該字段不能爲空');
複製代碼
StringType().isEmail('請輸入正確的郵箱地址');
複製代碼
StringType().isURL('請輸入正確的URL地址');
複製代碼
StringType().isOneOf(['Javascript', 'CSS'], '只能輸入 `Javascript`和 `CSS`');
複製代碼
StringType().containsLetter('必須包含英文字符');
複製代碼
StringType().containsUppercaseLetter('必須包含大寫的英文字符');
複製代碼
StringType().containsLowercaseLetter('必須包含小寫的英文字符');
複製代碼
StringType().containsLetterOnly('只能包含的英文字符');
複製代碼
StringType().containsNumber('必須包含數字');
複製代碼
StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, '請輸入合法字符');
複製代碼
StringType().rangeLength(6, 30, '字符個數只能在 6 - 30 之間');
複製代碼
StringType().minLength(6, '最小須要6個字符');
複製代碼
StringType().minLength(30, '最大隻能30個字符');
複製代碼
StringType().addRule((value, data) => {
return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, '請輸入合法字符');
複製代碼
NumberType().isRequired('該字段必填');
複製代碼
NumberType().isInteger('只能是整型');
複製代碼
NumberType().isOneOf([5, 10, 15], '只能是`5`,`10`,`15`');
複製代碼
NumberType().pattern(/^[1-9][0-9]{3}$/, '請輸入合法字符');
複製代碼
NumberType().range(18, 40, '請輸入 18 - 40 之間的數字');
複製代碼
NumberType().min(18, '最小值 18');
複製代碼
NumberType().max(40, '最大值 40');
複製代碼
NumberType().addRule((value, data) => {
return value % 5 === 0;
}, '請輸入有效的數字');
複製代碼
ArrayType().isRequired('該字段必填');
複製代碼
ArrayType().rangeLength(1, 3, '至少選擇1個,但不能超過3個');
複製代碼
ArrayType().minLength(1, '至少選擇1個');
複製代碼
ArrayType().maxLength(3, '不能超過3個');
複製代碼
ArrayType().unrepeatable('不能出現重複選項');
複製代碼
ArrayType().of(StringType().isEmail(), '格式錯誤');
複製代碼
ArrayType().addRule((value, data) => {
return value.length % 2 === 0;
}, '好事成雙');
複製代碼
DateType().isRequired('日期不能爲空');
複製代碼
DateType().range(
new Date('08/01/2017'),
new Date('08/30/2017'),
'時間應該在 08/01/2017 - 08/30/2017 之間'
);
複製代碼
DateType().min(new Date('08/01/2017'), '時間的最小值 08/01/2017');
複製代碼
DateType().max(new Date('08/30/2017'), '時間的最大值 08/30/2017');
複製代碼
DateType().addRule((value, data) => {
return value.getDay() === 2;
}, '只能選擇週二');
複製代碼
ObjectType().isRequired('該對象不能爲空');
複製代碼
ObjectType().shape({
email: StringType().isEmail('應該是一個 email'),
age: numberType().min(18, '年齡應該大於18歲')
});
複製代碼
ObjectType().addRule((value, data) => {
if (value.id || value.email) {
return true;
}
return false;
}, 'id 與 email 必須有一個不能爲空');
複製代碼
BooleanType().isRequired('該字段不能爲空');
複製代碼
ObjectType().addRule((value, data) => {
if (typeof value === 'undefined' && A === 10) {
return false;
}
return true;
}, '當 A 等於 10 的時候,該值必須爲空');
複製代碼