Describe property with constant value

5,991 views
Skip to first unread message

exavolt

unread,
Dec 19, 2011, 10:47:28 PM12/19/11
to json-...@googlegroups.com
How to describe a property with constant value? I've been searched for this but didn't find any clue.

For example, taken from an implementation of OAuth 2.0:

"token_type - Indicates the type of token returned. At this time, this field will always have the value 'Bearer'"

Chris Miles

unread,
Dec 20, 2011, 4:31:31 AM12/20/11
to json-...@googlegroups.com
You can do this with an enum with a single entry.

Chris

> --
> You received this message because you are subscribed to the Google
> Groups "JSON Schema" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/json-schema/-/cf1TRAmM3IsJ.
> To post to this group, send email to json-...@googlegroups.com.
> To unsubscribe from this group, send email to
> json-schema...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/json-schema?hl=en.

exavolt

unread,
Dec 20, 2011, 12:17:37 PM12/20/11
to json-...@googlegroups.com
Ah I see. Thank you.

Michael Nachtigal

unread,
May 28, 2013, 2:39:26 PM5/28/13
to json-...@googlegroups.com
Is there any interest in making a more elegant/"official" solution to this, perhaps by adding a new JSON Schema property, e.g., "constant" or "value"?

Geraint

unread,
May 29, 2013, 6:21:44 AM5/29/13
to json-...@googlegroups.com
Not from me, at least.  The current vocab is sufficient, and a new keyword is quite a big change, because it complicates every tool that deals with JSON Schemas.

What's more, I think the OAuth situation you have is a good example of when people might want this. I assume what's actually happening is they envisaged more than one possible value in future (which is quite obviously a job for "enum"), however there is only one option at the moment.  I actually think a single-valued enum is not an inelegant way to express that.

Michael Nachtigal

unread,
May 29, 2013, 8:01:52 AM5/29/13
to json-...@googlegroups.com
Thanks for the reply.

It definitely is functionally sufficient, but the syntax leaves something to be desired. The OAuth example was from another mailing list member, but in my situation, I have a json schema that describes objects that must have a property, and that property must always have value True, both for now and forevermore.

The single-element enum feels somewhat like disallowing base types like ints or bools in a programming language, instead insisting that every value has to have an array/list type, and the substitute for a simple int or bool would be an int list or bool list with a single element. Yes, it will work, but I would call that inelegant (or at least unpolished).

I totally understand the syntax and tool rewriting issues, though.

From: json-...@googlegroups.com [json-...@googlegroups.com] on behalf of Geraint [gerai...@gmail.com]
Sent: Wednesday, May 29, 2013 6:21 AM
To: json-...@googlegroups.com
Subject: Re: [json-schema] Describe property with constant value

You received this message because you are subscribed to a topic in the Google Groups "JSON Schema" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/json-schema/xtWD6PTjb8M/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Francis Galiegue

unread,
May 29, 2013, 8:12:50 AM5/29/13
to json-...@googlegroups.com
Hello,

On Wed, May 29, 2013 at 2:01 PM, Michael Nachtigal
<Michael....@catalinamarketing.com> wrote:
[...]
>
> The single-element enum feels somewhat like disallowing base types like ints
> or bools in a programming language, instead insisting that every value has
> to have an array/list type, and the substitute for a simple int or bool
> would be an int list or bool list with a single element. Yes, it will work,
> but I would call that inelegant (or at least unpolished).
>

I don't understand?

The syntax of the enum keyword is [ value1, value2...] where value1
and value2 can be any (arbitrarily complex) JSON value. It doesn't
mean the value of the property must be [ value1, value2 ].

Or did I misunderstand your point?
--
Francis Galiegue, fgal...@gmail.com
JSON Schema in Java: http://json-schema-validator.herokuapp.com

Michael Nachtigal

unread,
May 29, 2013, 9:31:31 AM5/29/13
to json-...@googlegroups.com
> The syntax of the enum keyword is [ value1, value2...] where value1
> and value2 can be any (arbitrarily complex) JSON value. It doesn't
> mean the value of the property must be [ value1, value2 ].

I think you misunderstood my point. What I would like to specify something like this:

"properties": {
"property1": {
"type": "boolean",
"value": "true"
}
}

In other words, the only valid instances of this schema must have:

{
"property1": true
}

But because there is no such schema property as "value", I use the following instead:

"properties": {
"property1": {
"type": "boolean",
"enum": ["true"]
}
}

Which is less elegant (IMHO). My analogy was to the following:

int main(void){
int x = 5;
printf("%d", x);
return 0;
}

vs.

int[] main(void){
int x[1] = {5};
printf("%d", x[0]);
return {0};
}

It isn't a perfect analogy, but I think it illustrates that sufficiency does not imply elegance.

Thanks,
Mike

Francis Galiegue

unread,
May 29, 2013, 9:38:29 AM5/29/13
to json-...@googlegroups.com
On Wed, May 29, 2013 at 3:31 PM, Michael Nachtigal
<Michael....@catalinamarketing.com> wrote:
>> The syntax of the enum keyword is [ value1, value2...] where value1
>> and value2 can be any (arbitrarily complex) JSON value. It doesn't
>> mean the value of the property must be [ value1, value2 ].
>
> I think you misunderstood my point. What I would like to specify something like this:
>
[...]

OK, so that's what I surmised.

This has already been discussed. I have also proposed "value" but it
has been deemed counterproductive. Honestly, I don't care one way or
another ;) But I stick to the majority consensus here.

frontend_dev

unread,
May 29, 2013, 4:39:32 PM5/29/13
to json-...@googlegroups.com

This has already been discussed. I have also proposed "value" but it
has been deemed counterproductive.

But why?

I also would like something like "value", I think it is more explicit and clear that in this case only _one_ possible value is allowed. I also use "value" in my slightly modified JSON Schema to "prefill" values in the model when having undefined data, but a defined "value" in the JSON schema. Useful when you want to create new objects with JSON Schema acting as a template. So in this case it acts like a kind of default. Also in this case, parsing "enum" would be a bit more complex.

frontend_dev

unread,
May 29, 2013, 4:52:34 PM5/29/13
to json-...@googlegroups.com

I also use "value" in my slightly modified JSON Schema to "prefill" values in the model when having undefined data, but a defined "value" in the JSON schema. Useful when you want to create new objects with JSON Schema acting as a template. So in this case it acts like a kind of default. Also in this case, parsing "enum" would be a bit more complex.

Forget about that part, of course there is already "default" - mixed that up

halmai

unread,
Aug 7, 2017, 4:35:28 AM8/7/17
to JSON Schema
I faced this issue quite recently. The problem is that specifying an object with some constant fields is much more complicated than it should be.
For example, in order to describe an object with two constant fields, I have to write this now:

{
    "schema":{
        "type":"object",
        "properties":{
            "result":{
                "type":"string",
                "enum":"[ERROR]"
            },
            "error_code":{
                "type":"string",
                "enum":"[ERROR__PET_NOT_FOUND]"
            }
        }
    }
}

With the new keyword "value" it would be just a bit simpler:

{
    "schema":{
        "type":"object",
        "properties":{
            "result":{
                "type":"string",
                "value":"ERROR"
            },
            "error_code":{
                "type":"string",
                "value":"ERROR__PET_NOT_FOUND"
            }
        }
    }
}

Not a huge win. But the value tells not just the value itself, it suggests the type as well in many cases. It would make the type field unnecessary according to the following rules. If the value is
- surrounded by " symbols then it would mean type = string,
- only digits would mean "integer"
- digits, decimal point and sign would mean "float"
- the values "true" and "false" (without " symbols) would mean boolean
- if it i surrounded by { and } symbols then it is an object.

In all cases, however, the type=... member still can be used to override the default, for example to tell that "1" should be treated as a float value rather than integer.
With this rules, the above specification gets even simpler:

{
    "schema":{
        "type":"object",
        "properties":{
            "result":{
                "value":"ERROR"
            },
            "error_code":{
                "value":"ERROR__PET_NOT_FOUND"
            }
        }
    }
}
 

Furthermore, the schema also could have a "value" field to get rid of the type and properties field. This would make the definition even shorter and more readable at the same time.

{
    "schema":{
        "value": {
            "result": "ERROR",
            "error_code": "ERROR__PET_NOT_FOUND"
        }
    }
}


Based on this, I think introducing the "value" with the described default effects, it would be a huge win.

Henry Andrews

unread,
Aug 7, 2017, 8:35:43 PM8/7/17
to json-...@googlegroups.com
The "const" property was added in draft-wright-json-schema-validation-01 (a.k.a. draft-06)
-henry



From: halmai <csongor...@gmail.com>
To: JSON Schema <json-...@googlegroups.com>
Sent: Monday, August 7, 2017 1:35 AM
Subject: [json-schema] Re: Describe property with constant value

--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


halmai

unread,
Aug 7, 2017, 9:02:57 PM8/7/17
to JSON Schema, andrew...@yahoo.com
fantastic, thanks. :)

Henry Andrews

unread,
Aug 10, 2017, 7:48:21 PM8/10/17
to json-...@googlegroups.com
Once again, we added "const" to draft-wright-json-schema-validation-01 a.k.a. draft-06 which was published several months ago.  Please take a look at the current draft.  If you replace "value" with "const" all of your examples are valid draft-06 schemas.

We do not specify type inference for either "const" or "enum", in part because a field that may be "const" in one context may have multiple possible types elsewhere.  For instance:

{
  "oneOf": [
    {"properties": {"foo": {"const": 1234}, ...}},
    {"properties": {"foo": {"const": null}, ...}}
  ]
}

However, an application may know that it is safe to infer type and is free to do so.

You don't need to convince anyone of anything.  We added this about six months ago.

thanks,
-henry




From: halmai <csongor...@gmail.com>
To: JSON Schema <json-...@googlegroups.com>
Sent: Monday, August 7, 2017 1:35 AM
Subject: [json-schema] Re: Describe property with constant value

Henry Andrews

unread,
Aug 10, 2017, 7:49:33 PM8/10/17
to json-...@googlegroups.com
Oops- ignore my slightly exasperated second response.  A filing error made it look like you re-asked the question :-P
My apologies, I'm a little under the weather today and should probably not be answering technical questions!

-henry



From: halmai <csongor...@gmail.com>
To: JSON Schema <json-...@googlegroups.com>
Cc: andrew...@yahoo.com
Sent: Monday, August 7, 2017 6:02 PM
Subject: Re: [json-schema] Re: Describe property with constant value

fantastic, thanks. :)

2017. augusztus 8., kedd 10:35:43 UTC+10 időpontban Henry Andrews a következőt írta:
The "const" property was added in draft-wright-json-schema- validation-01 (a.k.a. draft-06)
Reply all
Reply to author
Forward
0 new messages