mage-validator
Validation system for MAGE topics and user command input types (TypeScript).
Installation
npm install --save mage-validator class-validator class-transformer reflect-metadata
You need to install two peer dependencies alongside mage-validator
:
class-validator
so that you may add validation decorators to your data classclass-transformer
to control how the received data will be deserialisedrelfect-metadata
will be needed by the two modules above to extract type information
You will also need to make sure that the following configuration is set in your
tsconfig.json
:
Usage
mage-validator exports all functions exposed by class-validator and class-transformer for convenience. A few changes and additions have however been made for convenience.
@Type decorator
// Both are equivalent
The @Type
decorator has been customized to accept either a type
or a function (instead of only a function).
@MapOf decorator
mage-validator
also provides an additional @MapOf
decorator for dealing with
key-value map objects; using this type will both ensure nested maps entries
will be typed and that each entries will be validated (so @ValidateNested
is not required).
(Optional) Project structure
In normal MAGE projects, you need to put all your topics configuration under
./lib/archivist/index.ts
. However, this quickly becomes hard to manage as
the number of topics grows.
Instead, we recommend using the following file structure:
lib/ archivist/ index.ts modules/ modulename/ topics/ Players.ts types/ PlayerConfig.ts SomethingElse.ts usercommands/ addGems.ts commandTwo.ts index.ts index.ts
In other words, we recommend to separate MAGE topics and attach them to their
related modules under the topics
folder; we also recommend to put other
types (example: user command custom parameter type) under a types
folder.
To make it easier for you to set this up, mage-validator
comes with a method you
can simply drop into ./lib/archivist/index.ts
; it will scan each one of your
modules and attempt to load all topics defined in them.
./lib/archivist/index.ts
loadTopicsFromModulesexports
User command parameters and return data validation
Validation works by encapsulating messages into types:
./lib/modules/modulename/types/PlayerData.ts
;
Which then can be used as user commands types:
./lib/modules/modulename/usercommands/addGems.ts
In this case, both data
and gemRegisterBonus
will be validated prior to execution, and
data
will be validated once again once it is returned.
Topics
Topic definition
Topics work the same way as types, except that they contain the topic configuration as static parameters:
./lib/modules/modulename/topics/Player.ts
;;
If you wish to use tome instead, you can
you can extend the ValidatedTomeTopic
class instead. Unlike normal tomes
, you will
simply access and set object values directly (instead of using assign
, set
, valueOf
and so on).
Loading topics
./lib/modules/modulename/usercommands/getPlayer.ts
Storing topics
Topic instances also work as state wrappers:
./lib/modules/modulename/usercommands/createPlayer.ts
In this particular case, you could even simply put the player topic directly as you user command parameter; all you will then need to do is to set the index at some point before you attempt to record any operations:
Topics as user command parameters
./lib/modules/modulename/usercommands/createPlayer.ts
License
MIT.