forked from dowjones/react-json-schema-proptypes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (45 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import merge from 'lodash/merge';
import ajv from './ajvEx';
import omitDeprecated from './util';
export * as Schema from './schemas';
export fake from './fake';
export const SchemaSymbol = Symbol.for('react-json-schema-proptypes');
function name(component) {
return component.name || component.displayName;
}
function getSchema(schema) {
return schema[SchemaSymbol] || schema;
}
export function getComponentSchema(component) {
if (typeof component.propTypes === 'undefined')
throw new Error(`Component ${name(component)} has no propTypes.`);
if (typeof component.propTypes[SchemaSymbol] === 'undefined')
throw new Error(`Component ${name(component)} has no JSON Schema propType definition.`);
return omitDeprecated(component.propTypes[SchemaSymbol]);
}
export default function(mainSchema, ...otherSchemas) {
if (typeof mainSchema !== 'object') {
throw new TypeError('Schema must be of type \'object\'');
}
const schema = merge({}, getSchema(mainSchema), ...otherSchemas.map(getSchema));
if (schema.type !== 'object') {
throw new Error(`Schema must define an object type (currently: ${schema.type})`);
}
const propTypes = { [SchemaSymbol]: schema };
if (schema.properties) {
const validate = ajv.compile(schema);
for (let prop in schema.properties) {
if (schema.properties.hasOwnProperty(prop)) {
propTypes[prop] = function(props, propName, componentName) {
const valid = validate(props);
if (valid) return null;
const propError = validate.errors.find((e): boolean =>
new RegExp(`^\.${propName}(\.|$)`).test(e.dataPath));
if (!propError) return null;
return new Error(`'${propError.dataPath}' ${propError.message}, found ${JSON.stringify(props[propName])} instead. Check propTypes of component ${componentName}`);
};
}
}
}
return propTypes;
}