generated from hywax/vite-vanilla-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: schema validation is mandatory; error is thrown if schema is invalid
- Loading branch information
1 parent
27bcf55
commit d52f3f2
Showing
5 changed files
with
237 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,57 @@ | ||
# schemind | ||
[](https://www.npmjs.com/package/schemind) | ||
 | ||
[](https://bundlejs.com/?q=schemind&treeshake=%5B*%5D&config=%7B%22compression%22%3A%22brotli%22%7D) | ||
|
||
# schemind | ||
Read and write to messages serialized as arrays (aka indexed keys) by defining a schema. | ||
Read and write to messages serialized as arrays (aka indexed keys messages) by defining a schema. Protocol‑agnostic. | ||
|
||
## What? | ||
TODO | ||
In formats like JSON, a message normally looks something like this: | ||
```json | ||
{ | ||
"id": 1, | ||
"fullName": "John Doe", | ||
"email": "[email protected]", | ||
"birthDate": "1973-01-22", | ||
"address": { | ||
"street": "123 Main Street", | ||
"city": "Anytown", | ||
"zipcode": "12345-6789", | ||
"geo": { | ||
"lat": 42.1234, | ||
"lng": -71.2345 | ||
} | ||
}, | ||
"website": "www.johndoe.com" | ||
} | ||
``` | ||
*I'm using JSON as an example here, but schemind is essentially protocol-agnostic. I use it with MessagePack.* | ||
|
||
If you desperately need to make this message more compact, you could alternatively serialize it as such: | ||
```json | ||
[ | ||
1, | ||
"John Doe", | ||
"[email protected]", | ||
"1973-01-22", | ||
[ | ||
"123 Main Street", | ||
"Anytown", | ||
"12345-6789", | ||
[ | ||
42.1234, | ||
-71.2345 | ||
] | ||
], | ||
"www.johndoe.com" | ||
] | ||
``` | ||
|
||
This is sometimes referred to as a message with *indexed keys*. | ||
|
||
*Note that this obviously has some drawbacks: [recommended reading about the pros and cons of this format](https://github.com/MessagePack-CSharp/MessagePack-CSharp#use-indexed-keys-instead-of-string-keys-contractless).* | ||
|
||
**Schemind** helps you create and read such messages, if your (de)serializer doesn't support this technique. | ||
|
||
## Installation | ||
|
||
|
@@ -17,13 +62,23 @@ npm install schemind | |
## Usage | ||
TODO | ||
|
||
## FAQ | ||
TODO | ||
## FAQ | ||
### Shouldn't this be an extension of a serializer? | ||
Probably. | ||
|
||
### Wouldn't it be better to use protobuf at this point? | ||
Possibly. But if you're already using JSON / MessagePack / CBOR etc. in your app, and you need more compact messages for some features — *schemind* could be useful. | ||
|
||
Additionally, in some languages (backend or frontend) there's a MessagePack or JSON implementation that's faster, or allocates less memory, than protobuf. | ||
|
||
### Why is `get` so inconvenient? | ||
The `get` function prioritizes performance over convenience. The main goal here is to avoid any heap allocations (beyond what your deserializer allocates). I use *schemind* in performance-critical scenarios, where avoiding GC pauses is crucial. | ||
Use the `toPlainObject` function instead, if you don't mind some extra allocations. | ||
|
||
## Related work | ||
* https://github.com/MessagePack-CSharp/MessagePack-CSharp#use-indexed-keys-instead-of-string-keys-contractless | ||
* https://aarnott.github.io/Nerdbank.MessagePack/docs/customizing-serialization.html?q=indexed#serialize-objects-with-indexes-for-keys | ||
* [MessagePack-CSharp (.NET)](https://github.com/MessagePack-CSharp/MessagePack-CSharp#use-indexed-keys-instead-of-string-keys-contractless) | ||
* [Nerdbank.MessagePack (.NET)](https://aarnott.github.io/Nerdbank.MessagePack/docs/customizing-serialization.html?q=indexed#serialize-objects-with-indexes-for-keys) | ||
|
||
|
||
* https://github.com/Idein/msgpack-schema | ||
* https://github.com/serde-rs/serde/issues/959 | ||
* [Idein/msgpack-schema (Rust)](https://github.com/Idein/msgpack-schema) | ||
* [serde (Rust)](https://github.com/serde-rs/serde/issues/959) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { withIndex, get, set, toPlainObject, toIndexedKeysMessage } from './indexedKeysSchema' | ||
export { validateSchema, withIndex, get, set, toPlainObject, toIndexedKeysMessage, InvalidSchemaError } from './indexedKeysSchema' | ||
export type { ValidIndexedKeysMessageSchema } from './indexedKeysSchema' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.