成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

前端數(shù)據(jù)校驗從建模開始

韓冰 / 2525人閱讀

摘要:為了能夠把這部分代碼更有條理我們把數(shù)據(jù)校驗部分通過預(yù)先定義一個數(shù)據(jù)模型,把數(shù)據(jù)扔進(jìn)去,返回校驗結(jié)果。接下來我介紹一下這個工具,是一個數(shù)據(jù)建模及數(shù)據(jù)驗證工具它可以非常方便的設(shè)計的表單數(shù)據(jù)結(jié)構(gòu),當(dāng)然它不限于在表單使用。

前端開發(fā)過程中你們覺得處理什么業(yè)務(wù)功能最煩人?

做前端已經(jīng)有很長一段時間了,不知道大家是否和我有同樣的感受,在一些 Web 應(yīng)用中表單處理起來比其他功能模塊都麻煩,很多體力活,往往在數(shù)據(jù)的校驗會很費(fèi)時間。

為了能夠把這部分代碼更有條理,我們把數(shù)據(jù)校驗部分通過 Schema 預(yù)先定義一個數(shù)據(jù)模型,把數(shù)據(jù)扔進(jìn)去,返回校驗結(jié)果。

接下來我介紹一下這個工具,schema-typed 是一個數(shù)據(jù)建模及數(shù)據(jù)驗證工具, 它可以非常方便的設(shè)計的表單數(shù)據(jù)結(jié)構(gòu),當(dāng)然它不限于在表單使用。如果你在產(chǎn)品中使用了 React , 那配合 React Suite 的表單組件簡直就是如虎添翼。

安裝
npm install schema-typed --save
示例
import { SchemaModel, StringType, DateType, NumberType } from "schema-typed";

const userModel = SchemaModel({
  username: StringType().isRequired("用戶名不能為空"),
  email: StringType().isEmail("請輸入正確的郵箱"),
  age: NumberType("年齡應(yīng)該是一個數(shù)字").range(18, 30, "年齡應(yīng)該在 18 到 30 歲之間")
});

const checkResult = userModel.check({
  username: "foobar",
  email: "foo@bar.com",
  age: 40
});

console.log(checkResult);

checkResult 返回結(jié)構(gòu)是:

{
    username: { hasError: false },
    email: { hasError: false },
    age: { hasError: true, errorMessage: "年齡應(yīng)該在 18 到 30 歲之間" }
}
多重驗證
StringType()
  .minLength(6, "不能少于 6 個字符")
  .maxLength(30, "不能大于 30 個字符")
  .isRequired("該字段不能為空");
自定義驗證

通過 addRule 函數(shù)自定義一個規(guī)則。

如果是對一個字符串類型的數(shù)據(jù)進(jìn)行驗證,可以通過 pattern 方法設(shè)置一個正則表達(dá)式進(jìn)行自定義驗證。

const myModel = 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}$/, "請輸入合法字符")
});

schema.check({ field1: "", field2: "" });

/**
{
  field1: {
    hasError: true,
    errorMessage: "請輸入合法字符"
  },
  field2: {
    hasError: true,
    errorMessage: "請輸入合法字符"
  }
};
**/
自定義驗證 - 多字段交叉驗證

例如,驗證兩次輸入密碼是否一致

const schema = SchemaModel({
  password1: StringType().isRequired("該字段不能為空"),
  password2: StringType().addRule((value, data) => {
    if (value !== data.password1) {
      return false;
    }
    return true;
  }, "兩次密碼不一致")
});

schema.check({ password1: "123456", password2: "root" });

/**
{
  password1: { hasError: false },
  password2: {
    hasError: true,
    errorMessage: "兩次密碼不一致"
  }
};
**/
嵌套對象

對于復(fù)雜的嵌套的 Object , 可以使用 ObjectType().shape 方法進(jìn)行定義,比如:

const model = SchemaModel({
  id: NumberType().isRequired("該字段不能為空"),
  name: StringType().isRequired("用戶名不能為空"),
  info: ObjectType().shape({
    email: StringType().isEmail("應(yīng)該是一個 email"),
    age: numberType().min(18, "年齡應(yīng)該大于18歲")
  });
});

另外,更推薦把對象扁平化設(shè)計

import { flaser } from "object-flaser";

const model = SchemaModel({
  id: NumberType().isRequired("該字段不能為空"),
  name: StringType().isRequired("用戶名不能為空"),
  "info.email": StringType().isEmail("應(yīng)該是一個 email"),
  "info.age": numberType().min(18, "年齡應(yīng)該大于18歲")
});

const user = flaser({
  id: 1,
  name: "schema-type",
  info: {
    email: "schema-type@gmail.com",
    age: 17
  }
});

model.check(data);
API

StringType

NumberType

ArrayType

DateType

ObjectType

BooleanType

StringType

isRequired()

StringType().isRequired("該字段不能為空");

isEmail(errorMessage: string)

StringType().isEmail("請輸入正確的郵箱地址");

isURL(errorMessage: string)

StringType().isURL("請輸入正確的URL地址");

isOneOf(items: Array, errorMessage: string)

StringType().isOneOf(["Javascript", "CSS"], "只能輸入 `Javascript`和 `CSS`");

containsLetter(errorMessage: string)

StringType().containsLetter("必須包含英文字符");

containsUppercaseLetter(errorMessage: string)

StringType().containsUppercaseLetter("必須包含大寫的英文字符");

containsLowercaseLetter(errorMessage: string)

StringType().containsLowercaseLetter("必須包含小寫的英文字符");

containsLetterOnly(errorMessage: string)

StringType().containsLetterOnly("只能包含的英文字符");

containsNumber(errorMessage: string)

StringType().containsNumber("必須包含數(shù)字");

pattern(regExp: RegExp, errorMessage: string)

StringType().pattern(/^[1-9][0-9]{3}s?[a-zA-Z]{2}$/, "請輸入合法字符");

rangeLength(minLength: number, maxLength: number, errorMessage: string)

StringType().rangeLength(6, 30, "字符個數(shù)只能在 6 - 30 之間");

minLength(minLength: number, errorMessage: string)

StringType().minLength(6, "最小需要6個字符");

maxLength(maxLength: number, errorMessage: string)

StringType().minLength(30, "最大只能30個字符");

addRule(onValid: Function, errorMessage: string)

StringType().addRule((value, data) => {
  return /^[1-9][0-9]{3}s?[a-zA-Z]{2}$/.test(value);
}, "請輸入合法字符");
NumberType

isRequired()

NumberType().isRequired("該字段必填");

isInteger(errorMessage: string)

NumberType().isInteger("只能是整型");

isOneOf(items: Array, errorMessage: string)

NumberType().isOneOf([5, 10, 15], "只能是`5`,`10`,`15`");

pattern(regExp: RegExp, errorMessage: string)

NumberType().pattern(/^[1-9][0-9]{3}$/, "請輸入合法字符");

range(minLength: number, maxLength: number, errorMessage: string)

NumberType().range(18, 40, "請輸入 18 - 40 之間的數(shù)字");

min(min: number, errorMessage: string)

NumberType().min(18, "最小值 18");

max(max: number, errorMessage: string)

NumberType().max(40, "最大值 40");

addRule(onValid: Function, errorMessage: string)

NumberType().addRule((value, data) => {
  return value % 5 === 0;
}, "請輸入有效的數(shù)字");
ArrayType

isRequired()

ArrayType().isRequired("該字段必填");

rangeLength(minLength: number, maxLength: number, errorMessage: string)

ArrayType().rangeLength(1, 3, "至少選擇1個,但不能超過3個");

minLength(minLength: number, errorMessage: string)

ArrayType().minLength(1, "至少選擇1個");

maxLength(maxLength: number, errorMessage: string)

ArrayType().maxLength(3, "不能超過3個");

unrepeatable(errorMessage: string)

ArrayType().unrepeatable("不能出現(xiàn)重復(fù)選項");

of(type: Object, errorMessage: string)

ArrayType().of(StringType().isEmail(), "格式錯誤");

addRule(onValid: Function, errorMessage: string)

ArrayType().addRule((value, data) => {
  return value.length % 2 === 0;
}, "好事成雙");
DateType

isRequired()

DateType().isRequired("日期不能為空");

range(min: Date, max: Date, errorMessage: string)

DateType().range(
  new Date("08/01/2017"),
  new Date("08/30/2017"),
  "時間應(yīng)該在 08/01/2017 - 08/30/2017 之間"
);

min(min: Date, errorMessage: string)

DateType().min(new Date("08/01/2017"), "時間的最小值 08/01/2017");

max(max: Date, errorMessage: string)

DateType().max(new Date("08/30/2017"), "時間的最大值 08/30/2017");

addRule(onValid: Function, errorMessage: string)

DateType().addRule((value, data) => {
  return value.getDay() === 2;
}, "只能選擇周二");
ObjectType

isRequired()

ObjectType().isRequired("該對象不能為空");

shape(type: Object)

ObjectType().shape({
  email: StringType().isEmail("應(yīng)該是一個 email"),
  age: numberType().min(18, "年齡應(yīng)該大于18歲")
});

addRule(onValid: Function, errorMessage: string)

ObjectType().addRule((value, data) => {
  if (value.id || value.email) {
    return true;
  }
  return false;
}, "id 與 email 必須有一個不能為空");
BooleanType

isRequired()

BooleanType().isRequired("該字段不能為空");

addRule(onValid: Function, errorMessage: string)

ObjectType().addRule((value, data) => {
  if (typeof value === "undefined" && A === 10) {
    return false;
  }
  return true;
}, "當(dāng) A 等于 10 的時候,該值必須為空");

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/97242.html

相關(guān)文章

  • 0開始構(gòu)建一個屬于你自己的PHP框架

    摘要:如何構(gòu)建一個自己的框架為什么我們要去構(gòu)建一個自己的框架可能絕大多數(shù)的人都會說市面上已經(jīng)那么多的框架了,還造什么輪子。 showImg(https://segmentfault.com/img/bVNg9F?w=500&h=500); 如何構(gòu)建一個自己的PHP框架 為什么我們要去構(gòu)建一個自己的PHP框架?可能絕大多數(shù)的人都會說市面上已經(jīng)那么多的框架了,還造什么輪子?。我的觀點造輪子不是目...

    vpants 評論0 收藏0
  • 數(shù)據(jù)挖掘的TO-DO-LIST

    摘要:數(shù)據(jù)挖掘的流程與方法任務(wù)關(guān)聯(lián)分析聚類分析分類分析異常分析特異組群分析演變分析方法統(tǒng)計在線處理分析情報檢索機(jī)器學(xué)習(xí)分類實際應(yīng)用應(yīng)用分類趨勢預(yù)測推薦關(guān)聯(lián)類商品回歸分析實際應(yīng)用預(yù)測銷售趨勢聚類實際應(yīng)用分類關(guān)聯(lián)規(guī)則包括兩個階段從海量數(shù)據(jù)中找到高頻項 數(shù)據(jù)挖掘的流程與方法 1.任務(wù): 關(guān)聯(lián)分析 聚類分析 分類分析 異常分析 特異組群分析 演變分析 2.方法: 統(tǒng)計 在線處理分析 情報檢索 ...

    wangxinarhat 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<