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

Enum values are not validated #374

Open
lluiscab opened this issue Feb 8, 2023 · 1 comment
Open

Enum values are not validated #374

lluiscab opened this issue Feb 8, 2023 · 1 comment

Comments

@lluiscab
Copy link

lluiscab commented Feb 8, 2023

I've seen some examples on issues of people using a field of type enum to specify the page locale.

---
locale: 'en'
---
(...)
defineDocumentType(() => ({
    [...],
    fields: {
        locale: {
            type: 'enum',
            options: ['en', 'es', 'de'],
            required: true
        }
    }
}));

I've just seen on [email protected] that if you have a file with an invalid enum value (not in the options array), then the build will complete successfully.

---
locale: 'not in the options array'
---
(...)
$ yarn contentlayer build
yarn run v1.22.19
$ /(...)/node_modules/.bin/contentlayer build
Generated 2 documents in .contentlayer
Done in 1.32s.

I understand that even if it does not validate values, the enum type field is really useful once it automatically generates TS types, but given that we can validate fields with required, shouldn't we also be able to validate the if the field value matches what we defined on the schema? (In the case of enum fields)

@schickling schickling added this to the Pre-1.0 milestone Feb 13, 2023
@schickling schickling added feature New feature or request meta: never-stale labels Feb 13, 2023
@kouliavtsev
Copy link

kouliavtsev commented Jun 22, 2023

@schickling

This would be an amazing feature (warning value does not match enum) since you can import configuration files and validate if the content writer has picked the correct tags, categories, etc.

import someConfig from "./config/someConfig";

...
    test: {
      type: "enum",
      options: someConfig.map((item) => item.example),
      default: someConfig[1],
    },

The import someConfig from "./config/someConfig"; can now be used where you know that the values are valid.

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

No branches or pull requests

3 participants