Skip to content

Property initialization #53

@gregsdennis

Description

@gregsdennis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions