facebook / prop-types

Runtime type checking for React props and similar objects
MIT License
4.48k stars 356 forks source link

Custom prop-types not working inside shape #202

Closed nishant-labs closed 5 years ago

nishant-labs commented 6 years ago

Team,

I was trying to create a custom prop-types for an object using shape as below. Found that prop type for the property inside the shape (session) is not working, however same definition for className is working fine.

Test.propTypes = {
    configs: PropTypes.shape({
        url: PropTypes.string.isRequired,
        scope: PropTypes.string,
        session: (props, propName, componentName) => {
            if (typeof props[propName] === 'function') {
                return new Error(`${propName} must be a function!`);
            }
            return null;
        },
    }).isRequired,
    className: (props, propName, componentName) => {
        if (typeof props[propName] === 'string') {
            return new Error(`${propName} must be a string!`);
        }
        return null;
    },
};

I just copied above function to create custom validation.

Am I missing anything or its a bug!!

Thanks Nishant

ljharb commented 6 years ago

Looks right to me; can you share the component code and what’s not working?

nishant-labs commented 6 years ago

Can't share original code, however I will try to create a jfiddle

nishant-labs commented 6 years ago

Here is jfiddle link with basic example. I don't know why it's not working for className as well.

Thanks.

ljharb commented 6 years ago

Your className propType in the fiddle errors only when it is a string, so i assume you want !== there; i see the same error in the other one.

nishant-labs commented 6 years ago

@ljharb Thanks for pointing out, Got it why jfiddle was not working for className that was my mistake. I am new to jfiddle thing. However my original issue remains same and for 'session' it is still not working. updated jfiddle

ljharb commented 5 years ago

Your fiddle works fine when I use the non minified prop-types file - this is because the minified one is the production build, which does not actually do anything - you need to use the non-minified one.