schematox 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.json CHANGED
@@ -15,7 +15,15 @@
15
15
  "no-async-promise-executor": "off",
16
16
  "quotes": "off",
17
17
  "semi": "off",
18
- "@typescript-eslint/no-var-requires": "off"
18
+ "@typescript-eslint/no-var-requires": "off",
19
+ "@typescript-eslint/ban-types": [
20
+ "error",
21
+ {
22
+ "types": {
23
+ "{}": false
24
+ }
25
+ }
26
+ ]
19
27
  },
20
28
  "ignorePatterns": ["/dist"]
21
29
  }
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## [0.3.0](https://github.com/incerta/schematox/compare/v0.2.0...v0.3.0) (2024-05-04)
4
+
5
+ ### Features
6
+
7
+ - [`8bc2082`](https://github.com/incerta/schematox/commit/8bc208211457901f4f7246f00694f112d56f8d56) [#22](https://github.com/incerta/schematox/issues/22) Ensure optional properties in schemas reflect as optional in object context (@incerta)
package/README.md CHANGED
@@ -325,8 +325,19 @@ type FromStruct = SubjectType<typeof struct>
325
325
 
326
326
  ## Schema parameters
327
327
 
328
- - `optional?: boolean` – does `undefined` is valid value
329
- - `nullable?: boolean` – does `null` is valid value
328
+ - `optional?: boolean` –  unionize with `undefined`: `{ type: 'string', optinoal: true }` result in `string | undefined`
329
+
330
+ In the context of the object, optional values will be treated as optional properties:
331
+
332
+ ```typescript
333
+ const struct = object({ x: string().optional() })
334
+
335
+ type ExpectedSubjectType = {
336
+ x?: string | undefined
337
+ }
338
+ ```
339
+
340
+ - `nullable?: boolean` – unionize with `null`: `{ type: 'string', nullable: true }` result in `string | null`
330
341
  - `brand?: [string, string]` – make primitive type nominal "['idFor', 'User'] -> T & { \_\_idFor: 'User' }"
331
342
  - `minLength/maxLength/min/max` – schema type dependent limiting characteristics
332
343
  - `description?: string` – description of the particular schema property which can be used to provide more detailed information for the user/developer on validation/parse error
@@ -32,14 +32,35 @@ type Con_ArraySchema_SubjT<T extends ArraySchema> = T extends { of: infer U }
32
32
  : never
33
33
  : never
34
34
 
35
+ type Prettify<T extends Record<string, unknown>> = {
36
+ [K in keyof T]: T[K]
37
+ } & {}
38
+
39
+ /**
40
+ * ```MakeOptional{ x: T | undefined, y: T }```
41
+ *
42
+ * will make:
43
+ *
44
+ * ```{ x?: T | undefined, y: T }```
45
+ *
46
+ * NOTE: `Exclude<T[K], undefined> will not work
47
+ * so we have technical constraint here
48
+ **/
49
+ type MakeOptional<T> = Prettify<
50
+ {
51
+ [K in keyof T as undefined extends T[K] ? K : never]?: T[K]
52
+ } & {
53
+ [K in keyof T as undefined extends T[K] ? never : K]: T[K]
54
+ }
55
+ >
35
56
  type Con_ObjectSchema_SubjT<T extends ObjectSchema> = T extends {
36
57
  of: infer U
37
58
  }
38
- ? {
59
+ ? MakeOptional<{
39
60
  -readonly [k in keyof U]: U[k] extends Schema
40
61
  ? Con_Schema_SubjT<U[k]>
41
62
  : never
42
- }
63
+ }>
43
64
  : never
44
65
 
45
66
  type Con_UnionSchema_SubjT<T extends UnionSchema> = T extends {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "schematox",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Define JSON compatible schema statically/programmatically and parse/validate its subject with typesafety",
5
5
  "author": "Konstantin Mazur",
6
6
  "license": "MIT",