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

jsonschema - JSON Schema to reference to an array dynamically - Stack Overflow

programmeradmin3浏览0评论

I have a list of items in an array as below:

students:
  schoolName: "Maroubra Public"
  isEnrolled: true
  details:
    - name: sam
    - name: david
    - name: daniel
    - name: terrence
    - name: sylvia
    - name: annie
    - name: rob
    - name: john

I now another list for library membership and I would like to make sure that students requested for membership are within the previous list of students.

libMembership:
  - membershipType: gold
    members:
      - name: daniel
      - name: sylvia
      - name: emmanuel

I would like to the JSON schema validation failed because the name emmanuel in libMembership.members is not present in the students.details

In order to validate in the JSON schema, I used enum and tried various ways to reference students.details dynamically but nothing seemed to have worked for me, like so:

"enum": ["$data/students/details/*/name"]

The error I have been getting like:

<class 'jsonschema.exceptions.ValidationError'> 'emmanuel' is not one of ['$data/students/details/*/name']

It seems to have only picked up the invalid name but does not identify the wildcard to reference to the student names in the details (first list)

I have a list of items in an array as below:

students:
  schoolName: "Maroubra Public"
  isEnrolled: true
  details:
    - name: sam
    - name: david
    - name: daniel
    - name: terrence
    - name: sylvia
    - name: annie
    - name: rob
    - name: john

I now another list for library membership and I would like to make sure that students requested for membership are within the previous list of students.

libMembership:
  - membershipType: gold
    members:
      - name: daniel
      - name: sylvia
      - name: emmanuel

I would like to the JSON schema validation failed because the name emmanuel in libMembership.members is not present in the students.details

In order to validate in the JSON schema, I used enum and tried various ways to reference students.details dynamically but nothing seemed to have worked for me, like so:

"enum": ["$data/students/details/*/name"]

The error I have been getting like:

<class 'jsonschema.exceptions.ValidationError'> 'emmanuel' is not one of ['$data/students/details/*/name']

It seems to have only picked up the invalid name but does not identify the wildcard to reference to the student names in the details (first list)

Share Improve this question edited Feb 14 at 15:40 Jeremy Fiel 3,2972 gold badges11 silver badges25 bronze badges asked Feb 14 at 4:35 Sam AffanSam Affan 1
Add a comment  | 

1 Answer 1

Reset to default 0

Referencing instance data like this isn't supported out of the box with JSON Schema, however you can do it if you're in .Net by using JsonSchema.Net (my library) and its extension JsonSchema.Net.Data. You can see an example here.

If you want to play with it first, you can use my playground: https://json-everything/json-schema.

发布评论

评论列表(0)

  1. 暂无评论