最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Json Schema restrictions on Value field which can be Integer or String - Stack Overflow

programmeradmin0浏览0评论

I am currently having a JSON schema in my code

 {
    'type' => 'object', 'required' => true, 'additionalProperties' => false,
    'properties' => {
        'variables' => {
            'type' => 'array', 'required' => true,
            'items' => {
                'type' => 'object', 'required' => true, 'additionalProperties' => false,
                'properties' => {
                    'variable' => { 'type' => 'string', 'required' => true },
                    'value' => { 'required' => true }
                    }
                }
            }
        }
}

I am trying to put some extra validations here on value field. E.g. In case on String length should not be more than 64 chars , and in case of Integer value should be in Integer limit. how should I go about defining this schema.

I am currently having a JSON schema in my code

 {
    'type' => 'object', 'required' => true, 'additionalProperties' => false,
    'properties' => {
        'variables' => {
            'type' => 'array', 'required' => true,
            'items' => {
                'type' => 'object', 'required' => true, 'additionalProperties' => false,
                'properties' => {
                    'variable' => { 'type' => 'string', 'required' => true },
                    'value' => { 'required' => true }
                    }
                }
            }
        }
}

I am trying to put some extra validations here on value field. E.g. In case on String length should not be more than 64 chars , and in case of Integer value should be in Integer limit. how should I go about defining this schema.

Share Improve this question edited Feb 12, 2015 at 9:07 coDe murDerer 1,8764 gold badges21 silver badges28 bronze badges asked Feb 12, 2015 at 8:48 user2161522user2161522 2033 silver badges6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

For positive integer less or equal to 1024:

"type": "integer",
"minimum": 0,
"maximum": 1024

For string to be not empty and not longer than 64 symbols:

"type": "string",
"pattern": "^.{1,64}$"

There are good explanations of all the guards at official site.

发布评论

评论列表(0)

  1. 暂无评论