Coder Social home page Coder Social logo

danalexilewis / react-json-schema-proptypes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dowjones/react-json-schema-proptypes

0.0 1.0 0.0 68 KB

Define React propTypes with JSON schema, then introspect and fake props

License: MIT License

JavaScript 100.00%

react-json-schema-proptypes's Introduction

React-json-schema-proptypes

Build React PropTypes using a JSON Schema with introspection support.

Usage

npm install --save react-json-schema-proptypes

import propTypeSchema from 'react-json-schema-proptypes';
import React from 'react';

...

const schema = {
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    }
  }
};

class Article extends React.Component {
  static propTypes = propTypeSchema(schema)

  render() {
    ...
  }
}

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

import propTypeSchema, {getComponentSchema} from 'react-json-schema-proptypes';
import Image from 'components/image';
import Comment from 'components/comment';
import React from 'react';

...

class Article extends React.Component {
  static propTypes = propTypeSchema({
    "type": "object",
    "properties": {
      "image": getComponentSchema(Image), // Note: Things you compose must have been curated with react-json-schema-proptypes
      "comments": {
        "type": "array",
        "items": getComponentSchema(Comment)
      }
    }
  })

  render() {
    ...
  }
}

You can also update an existing schema by passing in objects that will override* it

import propTypeSchema from 'react-json-schema-proptypes';
import BaseArticle from '../components/base-article';
import React from 'react';

...

class Article extends React.Component {
  static propTypes = propTypeSchema(BaseArticle.propTypes, {
    "properties": {
      "newProperty": { "type": "string" }
    }
  }) // Note: extending things in this way requires them to have also have been curated with react-json-schema-proptypes

  render() {
    ...
  }
}

* This will create a new object rather than mutating existing ones so you're safe to use it however you want.

API

import createPropTypes, {getComponentSchema, Schema, SchemaSymbol} from 'react-json-schema-proptypes';

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.

Contributors

License

MIT

react-json-schema-proptypes's People

Contributors

charypar avatar mrchief avatar nemtsov avatar rgbboy avatar stuartharris avatar tmerlet avatar zhengl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.