0.0.2 • Published 7 years ago

json-format-check v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

JSON-FORMAT-CHECK

The Problem:

Everytime when you send some data to servicer, they will check your role, your authority then check your data, at last processing the data that you send to them.

For solving the role and authority problems, hard engineer invent the token\cookies mechanism. However for imporve the probability of success and imporve the experience of our website or application, we need to check the data that we send before we send it using some simply rules. Although we cannot determine what content user could input or manipulate, we can determine what data should be accepted by server, then we just need to tell user your input is illegality when necessary.

The Solution

Using this package and write some simple rules to check the format of JSON data.

wanted data

{
  name: 'students',
  number: 5,
  age:[10, 11, 12, 14, 15],
  info:[{
    name: 'A Student',
    sex: 'male',
    studentID: 00001
  },{
    name: 'B Student',
    sex: 'female',
    studentID: 00002
  },{
    name: 'C Student',
    sex: 'male',
    studentID: 00003
  },{
    name: 'D Student',
    sex: 'male',
    studentID: 00004
  },{
    name: 'E Student',
    sex: 'male',
    studentID: 00005
  }]
}

schema data

{
  name:{
    name: '姓名',
    type: 'String',
    required: true
  },
  number:{
    name: '学生数量',
    type: 'Number'
  },
  age:{
    name: '年龄',
    type: 'Array',
    items:{
      name: '年龄元素',
      type: 'Number',
      min: '18'
    },
    require: false
  },
  info:{
    name: '学生信息',
    type: 'Array'
  }
}

processing

import {JSONFormatCheck} from 'json-format-check'
console.log(JSONFormatCheck(schema_data, wanted_data)); 

you will get {success: true} then add part of schema_data into

school:{
  name: '学校',
  type: 'String',
  required: true
}

then you will get one error message

descript: "school is required, but not provided",
name : "学校",
param : "school", 
rule : "required", 
success : false

you can find that your json data is not pass the validation because the school attr is not provided. so you need to reminder the user to input this field.

All rules

  • each one attr should corresponding one attr with same name in scheme object
scheme attr namerequireddescription解释*
namerequiredused in return as one flag to quiclky find error data用于元素检查失败时返回的标志符
typerequiredtype of attr元素的类型Object, Array, Number, String, Null, Undefined, Regex, Date
requirednotattr is required? default is false元素是否必须填写,默认false,即不是必须存在true, false
allowNullnotallow value is Null? default is false是否允许null,默认false,即不允许true, false
allowUndefinednotallow value is Undefined? default is false是否允许undefined,默认false,即不允许true, false
enumnotvalue should be in one fixed data pool?元素可选的枚举
minnotthe min length or min value for String\Array or Number type value元素的最小长度或最小值,应用在Array、String、Numbe
maxnotthe max length or max value for String\Array or Number type value元素的最大长度或最大值,应用在Array、String、Numbe
patternnotvalue should test with one regex?用正则方式去检验元素
itemsnotshould test the item of one Array with shceme still?类型为Array时,用于确定数组内部元素格式的scheme Object
attrsnotshould test object attr with shceme still?类型为Object时,用于确定子对象元素格式的scheme Object