项目作者: vadirn

项目描述 :
Helps to define application state structure, default values and validate modifications
高级语言: JavaScript
项目地址: git://github.com/vadirn/state-model.git
创建时间: 2017-11-14T06:47:50Z
项目社区:https://github.com/vadirn/state-model

开源协议:MIT License

下载


state-model helps to define application state structure, default values and validate modifications. E.g. if app
state is too complex to remember it.

This is a es6 module. No compiled version is provided. Install via npm install state-model or yarn add state-model

API

  • const sm = new StateModel(definition: Object) - create new state model instance. Definition has a specific format
  • sm.set(state: Object, modifier: Object) => Object - validates modifier against definition, extends with default
    values if state doesn’t have them. Returns valid modifier or throws an error if modifier is invalid

Definition format

Every attribute of definition can have the following parameters:

  • __type - required. Possible values: “*“ - any type; “string”; “array”; “number”; “object”; “null”; “boolean”
  • __nullable - optional. If true, the attribute could have “null” value
  • __value - optional. Used as default value of the attribute (if __type is not “object”). If __type is “object”,
    but doesn’t have default value, it will throw an error. To assign any object set __type: '*'

  • PLANNED: __required - optional. Throw an error if modifier doesn’t contain required key

For example:

  1. {
  2. // root object should always be of object type
  3. __type: 'object',
  4. __value: {
  5. 'attr-1': {
  6. __type: '*',
  7. __value: {
  8. 'attr-2': {
  9. __type: 'string',
  10. __nullable: true
  11. }
  12. }
  13. },
  14. 'attr-3': {
  15. __type: '*',
  16. }
  17. }
  18. }

How to use?

  1. // In case you are using object-state-storage for state management
  2. import StateModel from 'state-model';
  3. const store = new ObjectStateStorage({ error: null });
  4. // for example store contains a list of items
  5. const sm = new StateModel({
  6. __type: 'object',
  7. __value: {
  8. '*': {
  9. __type: 'object',
  10. __value: {
  11. title: {
  12. __type: 'string',
  13. },
  14. timestamp: {
  15. __type: 'number',
  16. },
  17. display: {
  18. __type: 'boolean',
  19. __value: false,
  20. },
  21. },
  22. },
  23. error: {
  24. __type: 'string',
  25. __nullable: true,
  26. __value: null,
  27. },
  28. },
  29. });
  30. store.setState(state => sm.set(state, { id_1: { title: 'Hello world', timestamp: 1510737513759 } }));
  31. // store.state is going to be
  32. // {
  33. // id_1: { title: 'Hello world', timestamp: 1510737513759, display: false },
  34. // error: null,
  35. // }
  36. store.setState(state => {
  37. try {
  38. return sm.set(state, { id_1: { timestamp: '1510737513759' } });
  39. } catch (err) {
  40. return {
  41. error: err.toString(),
  42. };
  43. }
  44. });
  45. // store.state is going to be
  46. // {
  47. // error: 'Error: Type mismatch at "id_1.timestamp" ("number" expected, but "string" received)',
  48. // id_1: { title: 'Hello world', timestamp: 1510737513759, display: false },
  49. // }