Code Monkey home page Code Monkey logo

Comments (4)

mrmckeb avatar mrmckeb commented on July 20, 2024 1

Ah, I see - thanks again!

For our use case, we're almost always using @effect/schema to validate user-supplied data.

I'll look into how we can do that programatically, as we have a large number of schemas that I'd like to create JSON schemas for.

from effect.

gcanti avatar gcanti commented on July 20, 2024

JSONSchema.make(schema) returns a json schema for the To type of the provided schema, so in this case:

{
    readonly name: string;
    readonly age: number;
    readonly country?: string;
}

in the type above both name and age are required so you get required: [ 'name', 'age' ] which is correct

from effect.

mrmckeb avatar mrmckeb commented on July 20, 2024

Hi @gcanti, thanks for the prompt reply.

In our case we need a schema for from - as it's a schema for user-supplied data. Perhaps JSONSchema.make() could have an option for making the JSON schema using from or to?

We can make this work by using S.from(Person), however we then lose all annotations from the resulting schema. Do you have any suggestions to get this to work with annotations?

from effect.

gcanti avatar gcanti commented on July 20, 2024

Note that annotations are always added to the To side as well, so adding the annotation S.description('An optional age.') to the required field age is not appropriate.

What you want to do is add an annotation to a schema for user-supplied data (where the age field is optional) and then define a transformation that applies a default to the optional field to obtain a required field.

import * as S from "@effect/schema/Schema"
import * as JSONSchema from "@effect/schema/JSONSchema"

const PersonFrom = S.struct({
  name: S.string,
  age: S.optional(S.number, { exact: true }).pipe(S.propertySignatureAnnotations({ description: "An optional age." })),
  country: S.optional(S.string, { exact: true }).pipe(
    S.propertySignatureAnnotations({ description: "An optional country." })
  )
})

const PersonTo = S.struct({
  name: S.string,
  age: S.number,
  country: S.optional(S.string, { exact: true })
})

const Person = S.transform(
  PersonFrom,
  PersonTo,
  (pf) => ({ ...pf, age: pf.age ?? 0 }),
  (pt) => pt
)

console.log(JSONSchema.make(PersonFrom))
/*
{
  '$schema': 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  required: [ 'name' ],
  properties: {
    name: { type: 'string', description: 'a string', title: 'string' },
    age: {
      type: 'number',
      description: 'An optional age.',
      title: 'number'
    },
    country: {
      type: 'string',
      description: 'An optional country.',
      title: 'string'
    }
  },
  additionalProperties: false
}
*/

from effect.

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.