How to check for decimal number - yup

I have a schema:
const SignupSchema = Yup.object().shape({
decimal: Yup.number().integer('invalid decimal'),
});
I need to check if the number is a decimal, but I found only Integer in docs.

You can add a custom validation test e.g. with regex.
const SignupSchema = Yup.object().shape({
decimal: Yup.number().test(
'is-decimal',
'invalid decimal',
value => (value + "").match(/^\d*\.{1}\d*$/),
),
});

const digitsOnly = (value: string) => /^\d*[\.{1}\d*]\d*$/.test(value) || value.length === 0
const schema = yup.object().shape({
inputEntry: yup
.string()
.test('inputEntry', 'The field should have digits only', digitsOnly)
});
Above code is used for allowing only decimal,whole and null values in the input field.

just keep yup.number for decimals

Related

How to validate a non number field with Yup?

I have a field "company name" that must never contain any numeric characters at all and it should contain only string alphabetic characters, how to validate that with yup?
Thanks to #IAmGroot
const schema = yup.object().shape({
company_name: yup
.string()
.matches(/^[a-z]+$/, 'Only alphabetic characters allowed')
})

Warning: Operand of null-aware operation '??' has type 'int' which excludes null

Warning: Operand of null-aware operation '??' has type 'int' which excludes null. _countries.indexWhere((country)
onTap: () {
int index =
_countries.indexWhere((country) {
return country.startsWith(
alphabet.toUpperCase());
}) ??
0;
_listController.jumpTo(
index * 60.0,
);
},
child: Text(
alphabet,
style: TextStyle(
color: _theme.primaryColor,
fontSize: 14.0,
fontWeight: FontWeight.bold,
),
),
According to documentation, List.indexWhere always returns int - if the element isn't found, the return value is -1, but you seem to be thinking that it's null.
The documentation on indexWhere clearly states:
Returns -1 if element is not found.
So there is no possible way that your call returns null. The ?? operator only works on a nullable variable/expression, because it will return the result, or (in your case) 0 if the result is null. The result can never be null so your compiler tells you that that statement does not make any sense.
If you wanted the -1 to also be 0, you could use min to make sure the minimum you get is always 0.
indexWhere doesn't return nullable int. You might want firstWhere and then find index.
String item = _countries.firstWhere((country) {
return country.startsWith(alphabet.toUpperCase());
}, orElse: () => _countries.first);
int index = _countries.indexWhere((element) => element == item);
int selectedIndex = 0;
for (int i = 0; i < _countries.length; i++) {
if (_countries[i].startsWith(alphabet.toUpperCase())) {
selectedIndex = i;
break;
}
}
I hope there are better ways of doing it.

How to get the current field value in Yup when function

How should I get the password field value inside the when to compare the label value with the current value, so that I can strip the field?
*password: string().trim()
.notRequired()
.when(['$config', '$test', 'authenticationMethodType'],
(config: TestConfig, test: SyntheticPropertiesState,
authenticationMethodType: AuthenticationMethodType, schema: StringSchema):
StringSchema => {
const passwordPlaceholderCheck =
test?.authentication?.password !== Localize.PasswordPlaceholder;
if (passwordPlaceholderCheck) {
return schema.label(Localize.Password).required();
}
return schema.strip(true);
})
.max(63)
,*

specify field name instead of "value"

I have to validate value by value instead of passing in a whole schema for multiple values. Based on the docs for single value validation from here
https://hapi.dev/module/joi/
and this sample code
const validator: AnySchema = Joi.string().valid('foo', 'bar').required();
const validationResult: ValidationResult = validator.validate('invalid');
const validationError: ValidationError = validationResult.error;
if (validationError) {
throw validationError;
}
The code will throw an error with the following error message
ValidationError: "value" must be one of [foo, bar]
Is there an easy way I can replace "value" with a specific name? So when I want to validate environment the error message could be
ValidationError: "environment" must be one of [development, production,
test]
or is that only possible when validating multiple values at once?
There is the any.label(name) method that you can use and set a custom label that will be displayed in error messages as well:
any.label(name)
Overrides the key name in error messages.
name - the name of the key.
const schema = {
first_name: Joi.string().label('First Name')
};
You can simply do:
const validator: AnySchema = Joi
.string()
.label('Foo/Bar') // Change tha label using label method
.valid('foo', 'bar')
.required();
const validationResult: ValidationResult = validator.validate('invalid');
const validationError: ValidationError = validationResult.error;
if (validationError) {
throw validationError;
}
will output:
ValidationError: "Foo/Bar" must be one of [foo, bar]

Why mongoose schema type is double, but when insert value, type in mongo is int32?

I'm using mongoose-double to define Double type for mongoose.
My schema contain values property is an array of Double.
In pre-save middleware, init values is an array with 6 items 1000.
Check in mongo, 1000 type is int32
const mongoose = require('mongoose');
require('mongoose-double')(mongoose);
const Double = mongoose.Schema.Types.Double;
const test = new mongoose.Schema({
values: [Double]
})
test.pre('save', function(next) {
this.values = new Array(6).fill(1000),
})
I did what wrong ?
Well, in my opinion, you don't need to use Double.
As far as I know, there is 'Number' type in mongoose schema and it can be double, number and etc.
So use Number instead of Double.
const test = new mongoose.Schema({
values:Number
})
but in your case, you should use like this.
const test = new mongoose.Schema({
values:[Number]
})