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

question: optional fields #460

Closed
will-moore opened this issue Oct 27, 2021 · 2 comments
Closed

question: optional fields #460

will-moore opened this issue Oct 27, 2021 · 2 comments

Comments

@will-moore
Copy link
Contributor

Hi, I'm wondering if it's possible to mark a field as optional in the schema?

I was expecting something like:

fields:
  - name: foo
    type: string
    required: false

or

fields:
    foo?: string

The first is invalid and the second fails validation if foo field isn't found.

Is there some other way to achieve this, or is the scope of validation limited to required fields only?

Many thanks,
Will.

@tom-tan
Copy link
Member

tom-tan commented Oct 27, 2021

You can use null type for this purpose.
For example:

fields:
  - name: foo
    type: [string, "null"]

or

fields:
  - name: foo
    type: string?

Notes:

  • [string, "null"] represents a union type. It comes from Avro Schema.
  • Schema Salad considers a field with "null" type as optional.

@will-moore
Copy link
Contributor Author

That's great, thanks.
I had actually tried something like [string, "null"] as a list in yaml but not with the correct syntax.

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

2 participants