Coder Social home page Coder Social logo

Comments (2)

dodumosu avatar dodumosu commented on June 11, 2024

sorry, I wasn't very clear posting this. This idea is to expose the schema via an API endpoint so that another app can render a UI based on the schema. for something like a dropdown with labels and values, serialization like that would be necessary

from typical.

seandstewart avatar seandstewart commented on June 11, 2024

Hey @dodumosu -

Unfortunately, JsonSchema can't express enums in this way.

If I understand correctly, you want a schema something like this:

{
  "type": "array",
  "items": {
    "type": "object",
    "required": ["label", "value"],
    "additionalProperties": false,
    "properties": {
      "label": {"type": "string"},
      "value": {"type": "string"}
    }
  }
}

You could be exceptionally strict and create a unique object type for each enumerated choice, but that will be quite brittle and mean that with every addition or subtraction from your enumeration on the backend, you have a large schema migration on the frontend. In my experience, a solution like the above affords you some flexibility between backend and frontend while allowing your application to evolve.

It can also allow you to opt to change your storage format for these enumerations - perhaps one day you scale to a point where you want to make these choices configurable via updates from an operations team rather than engineers having to update them. In this scenario, you can migrate to storing these enumerations in a database and the frontend is none-the-wiser.

from typical.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.