1

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.
Ahmed Saeed
  • 635
  • 10
  • 27

1 Answers1

0

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.

Franrey Saycon
  • 647
  • 1
  • 5
  • 18