簡體   English   中英

我應如何針對模式驗證json

[英]how shall i validate json against schema

嗨,我正在使用MEAN STACK,我想針對貓鼬模式驗證來自前端的JSON密鑰。 我正在驗證值,但例如應如何驗證來自客戶端的com密鑰。

var CategorySchema = new Schema({
    name: {
        type: String,
        lowercase: true,
        default: '',
        trim: true,
        unique: [true, 'Category name already exists'],
        required: [true, 'Category Name cannot be blank'],
        minlength: [4, 'Minimum 4 characters required'],
        maxlength: [12, 'Category name cannot be That long']
    },
    parentCategory: {
        type: String,
        lowercase: true,
        default: '',
        trim: true
    },
    description: {
        type: String,
        lowercase: true,
        default: '',
        trim: true,
        required: [true, 'description cannot be blank'],
        minlength: [10, 'Very short description']
    },
    imageUrl: {
        type: String,
        default: '',
        trim: true
    }
});

如果我提供這種格式怎么辦

{

  "IMAGEURL": "c:\abc.png",         instead of imageUrl
  "DESCRIPTION": "here is some description", INSTEAD OF description
  "PARENTCATEGORY": "Men Wear", instead of parentcategory
  "Name": "Shirts" instead of name
}

我正在編寫將通過身份驗證的Rest API,是否有必要檢查這些東西。 友善的幫助

對我來說,JSON模式是如此復雜。 我建議使用Json模式驗證器

npm install jpv

它非常簡單,無需任何其他鍵即可使用,並且具有多種描述json的模式。

import jpv from 'jpv';

var json = {
  status : 'OK',
  data : {
    url : 'http://example.com'
  }
}

var pattern = {
  status : /^OK$/,
  data : {
    url : "[url]" 
  }
}

console.log( jpv.validate(json, pattern ) )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM