a11smiles
a11smiles

Reputation: 1258

Need some JSON Schema clarity

Let's say that my table structure look like this:

tblPhoneNumber
+-------------------+----------------------+ 
| Id                |  uniqueidentifier    | 
| Number            |  string              | 
| PhoneNumberTypeId |  smallint            | 
+-------------------+----------------------+

tblPhoneNumberType
+-------------------+----------------------+
| Id                |  smallint            |
| Type              |  string              |
| Description       |  string              |
+-------------------+----------------------+

My classes look like this:

public class PhoneNumber {
    public Guid Id { get; set; }
    public string Number { get; set; }
    public PhoneNumberType PhoneNumberType { get; set; }
}

public class PhoneNumberType {
    public short Id { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
}

My question is, what would be a valid JSON schema for the above? My end goal is to provide a public api while having the ability to use a SQL or NoSql repository. So I'd like to be able to validate the data.

My initial attempts at this are the following:

phoneNumber.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "PhoneNumber",
    "type": "object",
    "description": "A schema for a general phone number.",
    "properties": {
        "id": {
            "type": "object",
            "description": "A unique identifier."
        },
        "number": {
            "type": "string",
            "description": "The telephone number."
        },
        "phoneNumberType": { "$ref": "http://www.mywebsite.com/phoneNumberType#" },
    },
    "required": [
        "id",
        "number",
        "phoneNumberType"
    ]
}

phoneNumberType.json

{
    "id": "http://www.mywebsite.com/phoneNumberType#",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "PhoneNumberType",
    "description": "A phone number type enum.",
    "type": "object",
    "properties": {
    }
}

Specifically, my question is, in the phoneNumber.json, am I referencing phoneNumberType correctly? If not, how do i do so?

Thanks.

Upvotes: 3

Views: 68

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24409

Yes, what you have written is 100% standards compliant. However, different JSON Schema implementations manage references in different ways. Often there is an extra step involved to tell the library how you want references resolved. You will need to look into the documentation for the specific implementation you are working with to determine what you might be missing.

Upvotes: 1

Related Questions