我们如何验证书面模式是否有效。
const schema = {
"properties": {
"foo": { "add": "string" , "minLenfeffgth": 3, "maxLefngth": 255 }
}
};
上述架构是根据ajv.validateSchema()
的有效架构。
就像我们验证数据一样,任何函数都可以验证架构。
完整代码:
var Ajv = require('ajv');
var ajv = new Ajv({ allErrors: true});
const schema = {
"properties": {
"foo": { "add": "string" , "minLenfeffgth": 3, "maxLefngth": 255 }
}
};
// console.log(ajv.validateSchema(schema));
var validate = ajv.compile(schema);
test({"foo": ""});
function test(data) {
var valid = validate(data);
if (valid) console.log('Valid!');
else console.log(validate.errors);
}
结果是:有效
答案 0 :(得分:0)
您可以配置Ajv引发错误并使用from tkinter import *
top=Tk()
for t in [("1","2","3","4","5"),("aa","bb","cc","dd","ee"),("!@","%^","&*","@#","@$"),("A","B","C","D","E")]:
for x in range(4):
for y in range(5):
w = Text(top, width=15, height=2)
w.grid(row=x,column=y)
w.insert(END, t[y])
top.state("zoomed")
top.mainloop()
:
compile
var ajv = new Ajv({
allErrors: true
});
var schema = {
type: 'object',
properties: {
date: {
type: 'unexisting-type'
}
}
};
try {
var validate = ajv.compile(schema);
} catch (e) {
console.log(e.message);
}
答案 1 :(得分:0)
上述架构是根据ajv.validateSchema()的有效架构。
这是有效的,但没有进行任何验证,如果您想使用prefix = prefix or "\some_char"
强制属性来测试一个简单的对象,则可以执行以下操作:
data = {
point: {
x: '4.0',
y: '10.0'
}
}
startX = 10;
startY = 5;
newX= startX + data.point.x;
newY= startY - data.point.y;
console.log(newX, newY)
foo
运行数据= var ajv = new Ajv({
allErrors: true
});
var schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"foo": {
"type": "string",
"minLength": 3,
"maxLength": 255
}
},
"required": [
"foo"
]
};
try {
var validate = ajv.compile(schema);
test({"foo": "" });
} catch (e) {
console.log("Validate error :" + e.message);
}
function test(data) {
var valid = validate(data);
if (valid) {
console.log('Valid!');
} else {
console.log(validate.errors);
}
}
返回以下错误消息:
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.6.2/ajv.min.js"></script>
运行数据= {"foo": "" }
返回以下消息:
有效!