Coder Social home page Coder Social logo

JSON Schema const type about typebox HOT 4 CLOSED

sinclairzx81 avatar sinclairzx81 commented on May 16, 2024
JSON Schema const type

from typebox.

Comments (4)

sinclairzx81 avatar sinclairzx81 commented on May 16, 2024 2

@wyozi Hi, You can achieve this using Type.Literal(...). Example here and below

import { Type, Static } from '@sinclair/typebox'

const ItemAddedEvent = Type.Object({
  name: Type.Literal('item.added'),
  added: Type.String()
})

const ItemModifiedEvent = Type.Object({
  name: Type.Literal('item.modified'),
  modified: Type.String()
})

const Event = Type.Union([
  ItemAddedEvent,
  ItemModifiedEvent
])

function on(event: Static<typeof Event>) {
    switch(event.name) {
        case 'item.added': console.log(event.added); break;
        case 'item.modified': console.log(event.modified); break;
    }
}

Hope this helps

from typebox.

sinclairzx81 avatar sinclairzx81 commented on May 16, 2024

@seangenabe Hi, no reason in particular other than enum is expected to cast the widest net in terms of validators. However, I'd like to standardize on draft 6 (as a minimum requirement for validators). I am happy to accept PR's if you prefer to see const used over enum in the places where it's applicable.

Ill leave this issue open for the time being.
Cheers
S

from typebox.

sinclairzx81 avatar sinclairzx81 commented on May 16, 2024

Will close off this issue. For the time being keeping on with enum should be fine as it's largely identical to the singular const, but may review again in future. Thanks for letting me know.

from typebox.

wyozi avatar wyozi commented on May 16, 2024

Hello again :)

I have a usecase where I believe this would be required. I'm trying to build a type that can accept any of given disjoint unions:

const ItemAddedEvent = Type.Object({
  name: Type.Const('item.added'),
  // ...
})

const ItemModifiedEvent = Type.Object({
  name: Type.Const('item.modified'),
  // ...
})

const Event = Type.Union([
  ItemAddedEvent,
  ItemModifiedEvent
])

type X = Static<typeof Event>
// should be { name: 'item.added' } | { name: 'item.modified' }

Is there a way to achieve this currently or does this need const?

from typebox.

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.