React-json-schema-proptypes
Build React PropTypes using a JSON Schema with introspection support.
Usage
npm install --save react-json-schema-proptypes
;; ... const schema = "type": "object" "properties": "id": "type": "string" ; Component static propTypes = { ... }
Introspection
Creating propTypes
using react-json-schema-proptypes exposes the schema object on the component.
To get the schema, you can use the exported getComponentSchema
function.
Composing schemas from child components
;;;; ... Component static propTypes = { ... }
You can also update an existing schema by passing in objects that will override* it
;;; ... Component static propTypes = // Note: extending things in this way requires them to have also have been curated with react-json-schema-proptypes { ... }
* This will create a new object rather than mutating existing ones so you're safe to use it however you want.
API
;
createPropTypes(schema)
Given a JSON schema, return a React proptypes object.
getComponentSchema(Component)
Returns a component's schema from a component class.
SchemaSymbol
The Sympbol for react-json-schema-proptypes.
Schema
Schema.element
Is a schema that validates a React element. (Gives a schema represetation equivalent to React.Element)
Schema.node
Is a schema that validates a React node. (Gives a schema represetation equivalent to React.Node)
Schema.func
Is a schema that validates a function.
Caveats
Not all features of JSON-Schema are currently supported, for example cross-validation with other props, so we recommend keeping the schema simple and type-based rather than logic-based.