yup#StringSchema TypeScript Examples

The following examples show how to use yup#StringSchema. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: validation.ts    From remote-office-hours-queue with Apache License 2.0 7 votes vote down vote up
export function validateString (value: string, schema: StringSchema, showRemaining: boolean): ValidationResult {
    let transformedValue;
    let isInvalid = false;
    let messages = Array();
    try {
        transformedValue = schema.validateSync(value);
        const maxLimit = getMaxLimit(schema.describe());
        if (showRemaining && maxLimit) {
            messages.push(createRemainingCharsMessage({'value': transformedValue, 'max': maxLimit}));
        }
    } catch (error) {
        transformedValue = error.value;
        isInvalid = true;
        messages = error.errors;
    }
    return {'transformedValue': transformedValue, 'isInvalid': isInvalid, 'messages': messages};
}
Example #2
Source File: useValidation.ts    From remote-office-hours-queue with Apache License 2.0 6 votes vote down vote up
export function useStringValidation (schema: StringSchema, showRemaining?: boolean):
    [undefined | ValidationResult, (value: string) => ValidationResult, () => void]
{
    const [validationResult, setValidationResult] = useState(undefined as undefined | ValidationResult);
    const validateAndSetResult = (newValue: string) => {
        const result = validateString(newValue, schema, !!showRemaining);
        setValidationResult(result);
        return result;
    }
    const clearResult = () => setValidationResult(undefined);
    return [validationResult, validateAndSetResult, clearResult];
}
Example #3
Source File: validation.ts    From firecms with MIT License 5 votes vote down vote up
function getYupStringSchema({
                                property,
                                parentProperty,
                                customFieldValidator,
                                name
                            }: PropertyContext<StringProperty>): StringSchema {
    let schema: StringSchema<any> = yup.string();
    const validation = property.validation;
    if (property.config?.enumValues) {
        if (validation?.required)
            schema = schema.required(validation?.requiredMessage ? validation.requiredMessage : "Required");
        schema = schema.oneOf(
            enumToObjectEntries(property.config?.enumValues)
                .map(([key, _]) => key)
        );
    }
    if (validation) {
        schema = validation.required
            ? schema.required(validation?.requiredMessage ? validation.requiredMessage : "Required").nullable(true)
            : schema.notRequired().nullable(true);
        if (validation.unique && customFieldValidator && name)
            schema = schema.test("unique", "This value already exists and should be unique",
                (value, context) =>
                    customFieldValidator({
                        name,
                        property,
                        parentProperty,
                        value
                    }));
        if (validation.min || validation.min === 0) schema = schema.min(validation.min, `${property.title} must be min ${validation.min} characters long`);
        if (validation.max || validation.max === 0) schema = schema.max(validation.max, `${property.title} must be max ${validation.max} characters long`);
        if (validation.matches) schema = schema.matches(validation.matches, validation.matchesMessage);
        if (validation.email) schema = schema.email(`${property.title} must be an email`);
        if (validation.url) schema = schema.url(`${property.title} must be a url`);
        if (validation.trim) schema = schema.trim();
        if (validation.lowercase) schema = schema.lowercase();
        if (validation.uppercase) schema = schema.uppercase();
    } else {
        schema = schema.notRequired().nullable(true);
    }
    return schema;
}