Comments (8)
May be it's not good idea at all. From a server side view it's OK to remove optional flag because default value is injected at validation stage.
But if we use the same type definition for a client — the optional flag should remain.
from json-schema-to-typescript.
Anyway, the option I've suggested earlier will be useful. For example, in our development process we use two separate ts-files (with defaults injected - for server and without - for clients) converted from one json schema.
In this case client knows wich props are optional, and server thinks nothing optional because of injected defaults and we have less code in RPC methods.
from json-schema-to-typescript.
Just add them to the required lists? In the past I had experience that required was checked before default so didn't work necessarily but now I'm setting required and having default and it works even when not given (i.e. defaulted passes required check). The issue with the proposal indeed is what if useDefaults:true
is not given, required solves that.
from json-schema-to-typescript.
"default"
is an annotation keyword like "title"
or "description"
. it doesn't make sense in the context of typescript types, as there is no such thing as "default value" for a type. On AJV side it becomes a footgun, since its value is not validated, so its type also can drift away from the schema type. Also some values can't be serialized or not known at validation time, such as home directory, so the type being optional or not varies on case-by-case basis on top.
Your mistake is using json schema as data modeling DSL instead of JSON payload validation DSL. JSON, and therefore JSON Schema, will never be able to map neatly into types of other languages.
from json-schema-to-typescript.
Just add them to the required lists? In the past I had experience that required was checked before default so didn't work necessarily but now I'm setting required and having default and it works even when not given (i.e. defaulted passes required check). The issue with the proposal indeed is what if
useDefaults:true
is not given, required solves that.
Not really. Look — I use json schema files for:
- validate request on backend
- generate ts for backend
- generate ts for frontend
- generate docs (including defaults values!!!)
Also:
- I don't want to check at backend if optional param undefined and set to default in code. I want to do it by schema.
- I don't want to see at backend that the param is optional and use
args.param!
because I known — it is either sent by frontend or set to default by validator - I want to see param as optional at frontend.
You see?
So my solution (when I generate ts for back and front) — to treat default: ...
differently. For frontend I don't take it into account, only analyze "required:[...]" to see — optional or not. But for backend, I analyze default: ....
to see if param is never undefined.
from json-schema-to-typescript.
Your mistake is using json schema as data modeling DSL instead of JSON payload validation DSL.
Don't judge before you try. So, your advice — don't use defaults in json schema, put this into code? Produce more imperative code instead of declarative? Funny.
from json-schema-to-typescript.
it doesn't make sense in the context of typescript types, as there is no such thing as "default value" for a type.
Of course. But it does make sense in context: would be param undefined or not (event if it's not required). So it's related to treating param is optional or not regardless it's not in required:[]
from json-schema-to-typescript.
Related Issues (20)
- Crash for deprecated field missing description HOT 1
- A bit unclear about the purpose of `declareExternallyReferenced` and intention HOT 1
- Support ESM usage of module HOT 3
- Create `index.ts` when generating files from a directory HOT 1
- List on json-schema.org HOT 1
- Create Typscript schema with external references
- Convert empty object to Record<string,never> HOT 3
- `strictIndexSignatures` option is ignored for single pattern properties
- Package lists dev dependencies as dependencies HOT 4
- If `minProperties` is greater than or equal to the number of declared properties and `additionalProperties` is `false`, all properties should be required HOT 2
- properties + anyOf generates incorrect output
- `compile()` ignores `$refOptions.resolve` settings HOT 1
- What package manager does the repo use? HOT 6
- `SyntaxError` while building browser bundle HOT 2
- Add option to remove `[k: string]: unknown | undefined;` from generated types HOT 5
- Add yaml support HOT 3
- Static imports for $refs HOT 5
- bug in vercel production, but development environment is OK HOT 1
- "SyntaxError: Unexpected token in JSON at position 0" when running CLI on MacOS 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 json-schema-to-typescript.