JSON SCHEMA:我们可以对类型做一个或/(字符串/整数)吗? [英] JSON SCHEMA: Can we do an either/or (string/int) for type?

查看:519
本文介绍了JSON SCHEMA:我们可以对类型做一个或/(字符串/整数)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个架构来验证联系信息.
在电话号码验证中,我们有一个国家/地区代码.
我真的希望有一种方法可以使国家/地区代码成为stringinteger.

I'm doing a schema to validate contact information.
In the phone number validation, we have a country code.
I am really hoping there's a way to allow the country code to either be a string or an integer.

来自不同国家/地区的用户通知我,国家/地区代码中有时会包含一些有意义的非整数字符(例如"+").话虽如此,字符串才有意义.
另一方面,如果有人将国家/地区代码提供为int,例如.c之类的33(france),我觉得抛出一个故障警报并强迫他们进行铸造会有点荒谬,几乎是自命不凡.将其作为字符串,然后将其重新发送为"33".

Users from different countries inform me that there are sometimes meaningful non-integer characters in the country code (such as a "+"). That being said, a string makes sense.
On the other hand, if someone provides a country code as an int, say .. something like 33 (france), I feel it'd be a bit ridiculous, almost pretentious, to throw a failure alert and force them to cast it as a string and re-send it as "33".

我想强制执行类型约束,以确保某人不会尝试发送对象或数组,但是我需要允许它成为stringinteger之一.
该怎么办?

I would like to enforce type constraints to ensure that someone does not try to send an object or array, but I need to allow it to be one of either a string or an integer.
How can this be done?

推荐答案

您有几种选择,请使用

You have several options, please have a look at the docs:

  • 使用pattern属性,并使用正则表达式强制执行有效代码.
  • 将anyOf与两个模式一起使用.每个架构都会验证整数或字符串类型.
  • 如果您拥有所有国家代码(非常容易获得),则可以使用枚举列表强制使用确切的值.

这篇关于JSON SCHEMA:我们可以对类型做一个或/(字符串/整数)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆