Comments (4)
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.
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.
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.
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)
- `@effect/platform`s `Multipart` and `UrlParams` modules do not support `string[]` but should
- @effect/schema branded type error with recursion HOT 1
- Feature request: polymorphic `map` function HOT 1
- `@effect/platform`s `ClientRequest` (and possibly others) don't parse input urls
- `HttpClient` url utils accept a `URL` only in partial signatures
- From Discord: Encode optional hex byte array field as empty Uint8Array if undefined
- From Discord: Possible double initialization of a Layer in `Layer` construction
- From Discord: Workaround for Constructor Argument Inference with Optional Class Fields
- From Discord: Questions about SortedSet.fromIterable deduplication behavior HOT 1
- From Discord: Issue with renaming property in union schema
- From Discord: Issue with template literals type in @effect/[email protected] after upgrade
- From Discord: Schema template literal issue after upgrading to @effect/[email protected]
- JSR computability HOT 1
- From Discord: Setting response cookies from RPC calls HOT 2
- From Discord: Clarification on `onExcessProperty: preserve` Behavior: Key Order Not Maintained
- `Cause` pretty printing is broken for multi-line error messages
- Strict pattern matching HOT 7
- From Discord: Request for `Chunk.lastNonEmpty` Method in Code Library
- From Discord: Ensure `Chunk.reverse` Preserves NonEmpty Status in Output
- `Struct.evolve` no compile time error for non function transformers HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from effect.