Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property initialization #53

Open
gregsdennis opened this issue Sep 4, 2023 · 1 comment
Open

Property initialization #53

gregsdennis opened this issue Sep 4, 2023 · 1 comment

Comments

@gregsdennis
Copy link
Member

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
Copy link
Member Author

Related: json-schema-org/json-schema-spec#867

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant