當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript yup.addMethod函數代碼示例

本文整理匯總了TypeScript中yup.addMethod函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript addMethod函數的具體用法?TypeScript addMethod怎麽用?TypeScript addMethod使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了addMethod函數的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1:

import { getPasswordStrength } from './customValidators';

export const VALIDATIONS_MESSAGES = {
	REQUIRED: 'This field is required',
	TOO_SHORT_STRING: 'Must be at least ${min} characters',
	TOO_LONG_STRING: 'Must be at most ${max} characters',
	NOT_ALPHANUMERIC: 'Must use alphanumeric characters',
	DECIMAL: 'Must be a decimal number or integer',
	INTEGER: 'Must be an integer',
	USERNAME_CHARS: 'Must use only letters, numbers, hypens or underscores',
	NOT_NUMBER: 'Must be a number',
	MUST_BE_GREATER: 'Must be greater than or equal to  ${min}',
	MUST_BE_LOWER: 'Must be lower than or equal to ${max}'
};

Yup.addMethod(Yup.string, 'differentThan', differentThan );
Yup.addMethod(Yup.string, 'equalTo', equalTo);
Yup.addMethod(Yup.string, 'strength', strength );

/*
	Validation schemas
*/
export const schema = {
	firstName: Yup.string()
		.min(2, VALIDATIONS_MESSAGES.TOO_SHORT_STRING)
		.max(50, VALIDATIONS_MESSAGES.TOO_LONG_STRING)
		.required(VALIDATIONS_MESSAGES.REQUIRED),

	lastName: Yup.string().ensure()
		.min(2, VALIDATIONS_MESSAGES.TOO_SHORT_STRING)
		.max(50, VALIDATIONS_MESSAGES.TOO_LONG_STRING)
開發者ID:3drepo,項目名稱:3drepo.io,代碼行數:31,代碼來源:validation.ts

示例2: reach

} from "yup";

// reach function
let schema = yup.object().shape({
    nested: yup.object().shape({
        arr: yup.array().of(yup.object().shape({ num: yup.number().max(4) }))
    })
});
reach(schema, "nested.arr.num");
reach(schema, "nested.arr[].num");

// addMethod function
yup.addMethod<NumberSchema>(yup.number, "minimum", function(
    this,
    minValue: number,
    message: string
) {
    return this.min(minValue, message);
});
yup.addMethod(yup.date, "newMethod", function(
    this: yup.DateSchema,
    date: Date,
    message?: string
) {
    return this.max(date, message);
});

// ref function
schema = yup.object().shape({
    baz: yup.ref("foo.bar"),
    foo: yup.object().shape({
開發者ID:j-f1,項目名稱:forked-DefinitelyTyped,代碼行數:31,代碼來源:yup-tests.ts

示例3: reach

import { reach, date, Schema, ObjectSchema, ValidationError, MixedSchema, SchemaDescription, TestOptions, ValidateOptions } from 'yup';

// reach function
let schema = yup.object().shape({
    nested: yup.object().shape({
        arr: yup.array().of(
            yup.object().shape({ num: yup.number().max(4) })
        )
    })
  });
reach(schema, 'nested.arr.num');
reach(schema, 'nested.arr[].num');

// addMethod function
yup.addMethod(yup.date(), 'format', function(this: Schema) {
    return this.clone();
});

// ref function
schema = yup.object().shape({
    baz: yup.ref('foo.bar'),
    foo: yup.object().shape({
      bar: yup.string()
    }),
    x: yup.ref('$x')
});

schema.cast({ foo: { bar: 'boom' } }, { context: { x: 5 } });

// lazy function
const node: ObjectSchema = yup.object().shape({
開發者ID:mattlewis92,項目名稱:DefinitelyTyped,代碼行數:31,代碼來源:yup-tests.ts


注:本文中的yup.addMethod函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。