mol_schema 0.0.1

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/.nojekyll ADDED
File without changes
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # $mol_schema
2
+
3
+ > Runtime typing schemas
4
+
5
+ ## Schema API
6
+
7
+ - `toString()` - identity of schema.
8
+ - `check(val)` - type guard.
9
+ - `guard(val)` - strict parser.
10
+ - `cast(val)` - relaxed caster.
11
+ - `default` - default value for casting.
12
+
13
+ ## Leaf schemas
14
+
15
+ - `$mol_schema_enum(...options)` - some of constant values (first value as default).
16
+ - `$mol_schema_boolean` - true or false (default).
17
+ - `$mol_schema_string` - any string ("" as default).
18
+ - `$mol_schema_pattern(...regexp)` - string that matched to regexp ("" as default).
19
+ - `$mol_schema_float` - any number (NaN as default).
20
+ - `$mol_schema_integer` - integer number (0 as default).
21
+ - `$mol_schema_range(min,max)` - range of values between min (default) and max.
22
+ - `$mol_schema_positive` - value >= 0
23
+ - `$mol_schema_natural` - natural number from 0 (default).
24
+ - `$mol_schema_negative` - value <= 0
25
+
26
+ ## Composition schemas
27
+
28
+ - `$mol_schema_some(...variants)` - some of schemas (default of first as default).
29
+ - `$mol_schema_maybe(schema)` - value or null or undefined (undefined as default).
30
+ - `$mol_schema_every(...schemas)` - every of schemas for same value (first default casted through all of them).
31
+ - `$mol_schema_list(item)` - array of same types schema ([] as default).
32
+ - `$mol_schema_dict(key,val)` - key-value dictionary schema ({} as default).
33
+ - `$mol_schema_record({...fields})` - object with typed fields (object with default fields values by default).
34
+
35
+ ## Complex example
36
+
37
+ ```ts
38
+ export abstract class $my_user_main extends $mol_schema_dict({
39
+ name: $mol_schema_string,
40
+ age: $mol_schema_natural,
41
+ }) {}
42
+
43
+ export abstract class $my_user_full extends $mol_schema_dict({
44
+ ... $my_user_full.Fields,
45
+ bio: $mol_schema_string,
46
+ flags: $mol_schema_dict( $mol_schema_string, $mol_schema_boolean ),
47
+ }) {}
48
+
49
+ export abstract class $my_article_full extends $mol_schema_partial({
50
+ title: $mol_schema_string,
51
+ body: $mol_schema_string,
52
+ author: $my_user_full,
53
+ }) {}
54
+
55
+ export abstract class $my_article_search extends $mol_schema_some([
56
+ $mol_schema_dict({
57
+ data: $mol_schema_list( $my_article_full ),
58
+ }),
59
+ $mol_schema_dict({
60
+ fail: $mol_schema_pattern( /^\w+$/ ),
61
+ }),
62
+ ]) {}
63
+ ```
64
+
65
+ ```ts
66
+ response = $my_article_search.guard( response ) // Exception on wrong data
67
+ ```
68
+
69
+ ```ts
70
+ response = $my_article_search.cast( response ) // Defaults instead wrong data
71
+ ```
72
+
73
+ ```ts
74
+ if( $my_user_main.check( user ) ) {
75
+ // user is $my_user_main now
76
+ }
77
+ ```
78
+
79
+ ```ts
80
+ const article = $my_article_full.default // default article state
81
+ ```
package/manifest.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "mol_schema_schema",
3
+ "short_name": "mol_schema_schema",
4
+ "description": "",
5
+ "id": "mol_schema_schema",
6
+ "start_url": ".",
7
+ "display": "standalone",
8
+ "background_color": "#000000",
9
+ "theme_color": "#000000"
10
+ }
package/node.audit.js ADDED
@@ -0,0 +1,2 @@
1
+ console.info( `%cplace: $mol_build
2
+ message: Audit passed`, 'color:forestgreen; font-weight:bolder' )