使用 AJV 在 json 架构中进行空值验证



我正在使用Ajv来验证我的JSON数据。我找不到一种方法来验证空字符串作为键的值。我尝试使用模式,但它没有抛出适当的消息。

这是我的架构

{
"type": "object",
"properties": {
"user_name": { "type": "string" , "minLength": 1},
"user_email": { "type": "string" , "minLength": 1},
"user_contact": { "type": "string" , "minLength": 1}
},
"required": [ "user_name", 'user_email', 'user_contact']
}

我正在使用 minLength 来检查值是否应至少包含一个字符。但它也允许留出空白空间。

你可以做:

ajv.addKeyword('isNotEmpty', {
type: 'string',
validate: function (schema, data) {
return typeof data === 'string' && data.trim() !== ''
},
errors: false
})

在 json 架构中:

{
[...]
"type": "object",
"properties": {
"inputName": {
"type": "string",
"format": "url",
"isNotEmpty": true,
"errorMessage": {
"isNotEmpty": "...",
"format": "..."
}
}
}
}

我找到了另一种使用"not"关键字和"maxLength"的方法:

{
[...]
"type": "object",
"properties": {
"inputName": {
"type": "string",
"allOf": [
{"not": { "maxLength": 0 }, "errorMessage": "..."},
{"minLength": 6, "errorMessage": "..."},
{"maxLength": 100, "errorMessage": "..."},
{"..."}
]
},
},
"required": [...]
}

不幸的是,如果有人用空格填充字段,它将是有效的,因为空格算作字符。这就是为什么我更喜欢ajv.addKeyword('isNotEmpty',...(方法,它可以在验证之前使用trim((函数。

干杯!

目前,AJV 中没有内置选项可以执行此操作。

现在可以使用 ajv-keywords.
它是可用于 ajv 验证器的自定义模式的集合来实现。

将架构更改为

{
"type": "object",
"properties": {
"user_name": {
"type": "string",
"allOf": [
{
"transform": [
"trim"
]
},
{
"minLength": 1
}
]
},
// other properties
}
}

使用 ajv 关键字

const ajv = require('ajv');
const ajvKeywords = require('ajv-keywords');
const ajvInstance = new ajv(options);
ajvKeywords(ajvInstance, ['transform']);

transform 关键字指定在验证之前要执行的转换。

我做了与 Ronconi 相同的事情,但想强调如何使用模式,例如"不检查逻辑"。

ajv.addKeyword({
keyword: 'isNotEmpty',    
validate: (schema , data) => {
if (schema){
return typeof data === 'string' && data.trim() !== ''
}
else return true;
}
});
const schema = {
type: "object",
properties: {
fname: {
description: "first name of the user",
type: "string",
minLength: 3,
isNotEmpty: false,
},
}

基于@arthur-ronconi的答案,这是另一个在Typescript中工作的解决方案,使用最新版本的Ajv(文档(:

import Ajv, { _, KeywordCxt } from "ajv/dist/jtd";
const ajv = new Ajv({ removeAdditional: "all", strictRequired: true });
ajv.addKeyword({
keyword: 'isNotEmpty',
schemaType: 'boolean',
type: 'string',
code(cxt: KeywordCxt) {
const {data, schema} = cxt;
if (schema) {
cxt.fail(_`${data}.trim() === ''`);
}
},
error: {
message: 'string field must be non-empty'
}
});

最新更新