是否在PHP中使用另一个JSON来验证JSON对象

发布于 2025-02-10 07:45:43 字数 1143 浏览 0 评论 0原文

请我有一个表单参数如下。我正在尝试根据下面的模式构建动态形式。在我的数据库中,我有一个表列,即JSON。我试图指导不要输入任何数据,除了在“对象”表格中定义的数据

formDefinition = [
          {
            "key": "name",
            "value": "",
            "datatype": "string"
           },
           {
            "key": "sex",
            "value": " ",
            "datatype": "choice"
           },
           {
            "key": "occupation",
            "value": "",
            "datatype": "string"
           },
           {
            "key": "isRetired",
            "value": ,
            "datatype": "boolean"
           }
        ]

表:员工和字段名称为详细信息是JSON类型。

details = [{}]

当用户填写表单并提交时,如下所示。

details = [{"name": "something something"}, {"sex": "male"}, {"occupation": "something" }]

我需要一些验证检查,以查看详细信息是否具有与表格中定义的键相同的键,否则应该丢弃错误。

我没有尝试过此代码,因为我不确定是否可能。只是为了说明我要实现的目标。

public function dataValidator($data)
{
  foreach($data as $key => $value){
    foreach(formDefinition as form){
       if($key !== form['key']){
          return "invalid"
        }
     }
  }
}

Please I have a form parameter as follows. I am try to build a dynamic form based on the schema below. In my database, I have a table column which is json. I am trying to guide against entering anyhow data except those ones defined in the form array of objects

formDefinition = [
          {
            "key": "name",
            "value": "",
            "datatype": "string"
           },
           {
            "key": "sex",
            "value": " ",
            "datatype": "choice"
           },
           {
            "key": "occupation",
            "value": "",
            "datatype": "string"
           },
           {
            "key": "isRetired",
            "value": ,
            "datatype": "boolean"
           }
        ]

Table: employee and field name is details which is a json type.

details = [{}]

When the user fills the form and on submission, it would look as follows.

details = [{"name": "something something"}, {"sex": "male"}, {"occupation": "something" }]

I need some of validation check to see if the details has the same keys as it is defined in the form otherwise it should throw error.

I haven't tried this code as I am not sure if it is possible. Just to illustrate what I am trying to achieve.

public function dataValidator($data)
{
  foreach($data as $key => $value){
    foreach(formDefinition as form){
       if($key !== form['key']){
          return "invalid"
        }
     }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可遇━不可求 2025-02-17 07:45:43

看起来还不错。但是,只需要一个循环:

public function dataValidator($data)
{
    foreach($data as $key => $value) {
        $valid = current(array_filter($formDefinition, function($form) use($key) { return $form->key === $key; }));
        if(!$valid) {
            return 'invalid';
        }
    }
}

下次短提示:尝试一下即可。这可能避免了不必要的问题,因为您可能已经从结果中获得了答案。如果没有,您至少有足够的调试信息与我们分享;-)

Doesn't look that bad. However, only one loop is required:

public function dataValidator($data)
{
    foreach($data as $key => $value) {
        $valid = current(array_filter($formDefinition, function($form) use($key) { return $form->key === $key; }));
        if(!$valid) {
            return 'invalid';
        }
    }
}

Short tip for next time: Just try it out. That might avoid unnecessary questions since you might already get the answers from the result. And if not, you at least have sufficient debugging information to share with us ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文