lightsofapollo / joi-to-json-schema

140 stars 39 forks source link

How to create const? #32

Closed AkiKorpua closed 6 years ago

AkiKorpua commented 6 years ago

Seem's there is no support for const type in schema? I create following joi rule with only one valid value:

myfield: Joi.equal('Ingredient').required()

Now this is generated:

        "myfield": {
            "enum": [
                "Ingredient"
            ]
        }

What I would like to see:

        "myfield": {
            "const":  "Ingredient"
        }

Could this be doable?

raisch commented 6 years ago

What would the value be adding a new "const" type? If you can validate a field as a required enum with a single value, how would that differ from a "const"?

raisch commented 6 years ago

And for the record, 'const' really isn't a type, per se. It's a qualifier that indicates that a value of type T is immutable, irrespective of its type.

AkiKorpua commented 6 years ago

Ultimate goal here is to use joi notation to create dynamic json-schema to actually create form's with https://github.com/mozilla-services/react-jsonschema-form . On that there is difference in using Enum with only one option or Const.

As joi only give you synonyms like any.valid(value) - aliases: only, equal there is no exact keyword here.

For human it sounds like using keyword "only" would result in "const" but this is not valid assumption to Joi API.

Only way to find out that we really want const and not Enum is that there is only one option for valid/only/equal.

Does this sound like a hack?

raisch commented 6 years ago

While perhaps not exactly a hack, it does strike me that your request is really an issue best addressed at the application-side rather than something with which joi-to-json-schema should concern itself. So, I'm happy to defer to James (lightsofapollo) on this.

-- Rob Raisch, Internaut and Technocrat

On Mon, Jan 22, 2018 at 4:58 AM, AkiKorpua notifications@github.com wrote:

Ultimate goal here is to use joi notation to create dynamic json-schema to actually create form's with https://github.com/mozilla- services/react-jsonschema-form . On that there is difference in using Enum with only one option or Const.

As joi only give you synonyms like any.valid(value) - aliases: only, equal there is no exact keyword here.

For human it sounds like using keyword "only" would result in "const" but this is not valid assumption to Joi API.

Only way to find out that we really want const and not Enum is that there is only one option for valid/only/equal.

Does this sound like a hack?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/lightsofapollo/joi-to-json-schema/issues/32#issuecomment-359375572, or mute the thread https://github.com/notifications/unsubscribe-auth/AAzZy4DtyvJxz-RI8HDnqexq-3ojQuzDks5tNFvbgaJpZM4Ri5b5 .

AkiKorpua commented 6 years ago

Let's solve this other way then.