vocab-idl icon indicating copy to clipboard operation
vocab-idl copied to clipboard

Property initialization

Open gregsdennis opened this issue 1 year ago • 1 comments

Many languages allow for an initial value to be set for a property upon instantiation. For example, in C# we do this:

class MyClass
{
    public string Foo { get; set; } = "an initial value";
}

Although in a validation context, default doesn't really serve any purpose, in a generation context, it can be quite useful. The above as a JSON Schema could be written as:

{
  // ...
  "type": "object",
  "properties": {
    "foo": {
      "type": "string",
      "default": "an initial value"
    }
  }
}

(While validating that the default value meets the requirements of the schema that contains it isn't possible with meta-schema validation, I think a linter could probably pick it up.)

One caveat to this is that languages that do support this typically require that the initialization values are compile-time constants, so generally they're limited to strings, booleans, numbers, etc (no complex objects). There are generally other mechanisms for initializing more complex values.

gregsdennis avatar Sep 04 '23 01:09 gregsdennis