简体   繁体   中英

Using validate.js to validate my TextInput results in undefined

I am trying to use validate.js to manage my Textfields validations and after installing validate.js and importing it as import validate from 'validate.js'; then adding it to my InputField it seems that the result is undefined .

I tried to reproduce the issue with Expo snack but the following error appears Device: (953:881) Unable to resolve module 'module://validate.js' here is the Expo link:

https://snack.expo.io/@ahmedsaeed/validatetest

Do I miss something here and is there a better way to validate my form?

Here is my code snippet:

import React, { useState, useEffect } from 'react';
import { View } from 'react-native';
import { HelperText } from 'react-native-paper';
import { InputField } from '../../../GlobalReusableComponents/TextFields';
import validate from 'validate.js';

const SkillsAndExperienceScreen = (props) => {
    const [company, setCompany] = useState('');

    const constraints = {
        company: {
            presence: true
        }
    };

    const onCompanyChange = (val) => {
        const result = validate({company: val}, constraints);
        setCompany(val);
    }

    return (
        <View>
            <InputField
                onChangeText={(val) => onCompanyChange(val)}
                value={company}
                placeholder='Company'
            />
        </View>
    );
}

export default SkillsAndExperienceScreen;

const Style = {
    container: {
        flex: 1,
        backgroundColor: '#f8f9f9'   
    }
};

Thanks in advance.

Update: It seems that the project is showing me the same error

Unable to resolve module `validate` from `/Projects/Seem/src/screens/CompleteYourProfileScreens/SkillsAndExperienceScreen/index.js`: Module `validate` does not exist in the Haste module map.

Looking at the web, it seems import validate from "validate-js" is the way to go. The npm module is https://www.npmjs.com/package/validate-js . I tried it on your expo snack and it worked. But, i think that's an entirely different module.

There might be an expo snack specific problem on it, prolly the ".js" name perhaps?, not quite sure. I tried installing locally to project of mine and it worked.

I would suggest using https://www.npmjs.com/package/@hapi/joi since it's always has been my go to for object validation and it has much better community support, and updates are more frequent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM