1 Star 0 Fork 1

fangminglee/Informed

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
temp.js 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
// Node.js require:
const Ajv = require('ajv');
// const ajv = new Ajv({ allErrors: true, jsonPointers: true });
// require('ajv-errors')(ajv);
const ajv = new Ajv({ allErrors: true });
const schema = {
type: 'object',
required: ['name'],
properties: {
name: {
type: 'string',
title: 'First name',
'ui:control': 'input',
maxLength: 5
},
brother: {
type: 'object',
properties: {
foo: {
type: 'string',
title: 'First name',
'ui:control': 'input'
},
bar: {
type: 'number',
title: 'First name',
'ui:control': 'input',
'input:props': {
type: 'number'
}
}
}
},
age: {
type: 'number',
title: 'First name',
'ui:control': 'input',
'input:props': {
type: 'number'
}
},
bio: {
type: 'string',
title: 'Bio',
'ui:control': 'textarea'
},
authorize: {
type: 'string',
title: 'Authorize',
'ui:control': 'checkbox',
errorMessage: {
type: 'Invalid format'
}
},
color: {
type: 'string',
title: 'Color',
'ui:control': 'select',
oneOf: [
{
const: '',
title: '- Select -',
'input:props': {
disabled: true
}
},
{ const: 'red', title: 'Red' },
{ const: 'black', title: 'Black' },
{ const: 'white', title: 'White' }
]
},
model: {
type: 'string',
title: 'Model',
'ui:control': 'radio',
oneOf: [
{ const: 'ms', title: 'Model S' },
{ const: 'm3', title: 'Model 3' },
{ const: 'mx', title: 'Model X' },
{ const: 'my', title: 'Model Y' }
],
default: null,
'informed:props': {
initialValue: 'm3'
}
},
cars: {
type: 'array',
title: 'Cars',
'ui:control': 'select',
'input:props': {
multiple: true,
style: { height: '100px', width: '200px' }
},
items: {
oneOf: [
{ const: 'tesla', title: 'Tesla' },
{ const: 'volvo', title: 'Volvo' },
{ const: 'audi', title: 'Audi' },
{ const: 'jeep', title: 'Jeep' }
]
},
'informed:props': {
initialValue: ['jeep', 'tesla']
}
}
}
};
const data = {
model: 'm3',
cars: ['jeep', 'tesla'],
// name: 'Joe Puzzo',
age: 26,
bio: 'Hello',
authorize: true,
color: 'red'
};
const validate = ajv.compile(schema);
const valid = validate(data);
console.log(validate.errors);
console.log('KEYS', Object.keys(schema.properties));
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/fangminglee/Informed.git
git@gitee.com:fangminglee/Informed.git
fangminglee
Informed
Informed
master

搜索帮助