Usage with React

Thanks to the fact validate function returns input values (basically proxies them) it's very convenient to use for React.js action creators.

Let's take for example an extract from Puppetry domain model:

This model can be expressed with byContract interfaces in a separate module like that:

./interface/index.js

export const ENTITY_REF = {
  id: "string"
}

export const GROUP_REF = {  
  ...ENTITY_REF
};

export const TEST_REF = {
  ...ENTITY_REF,
  groupId: "string"
};


export const ENTITY = {
  editing: "boolean=",  
  disabled: "boolean="
};

export const GROUP = {
  title: "string=",
  tests: "*[]",
  ...GROUP_REF
};

export const TEST = {
  title: "string=",
  commands: "*[]",
  ...TEST_REF
};

We import the defined interfaces as I interface and use to validate action arguments ./actions/entity.js

import { validate } from "bycontract";
import * as I from "interface";

export const addGroup = ( data, ref = null ) => ({
  type: constants.ADD_GROUP,
  payload: {
    data: validate( data, { ...I.ENTITY, ...I.GROUP } ),
    ref: validate( ref, I.GROUP_REF )
  }
});

export const addTest = ( data, ref = null ) => ({
  type: constants.ADD_TEST,
  payload: {
    data: validate( data, { ...I.ENTITY, ...I.TEST } ),
    ref: validate( ref, I.TEST_REF )
  }
});

export const removeTest = ( ref ) => ({
  type: constants.REMOVE_TEST,
  payload: {
    ref: validate( ref, I.TEST_REF )
  }
});

Last updated