Question3r
Question3r

Reputation: 3802

Validate 'real' float field for digits precision

I want to validate coordinates. Those will be stored as real data columns, from the Postgres docs

real 4 bytes variable-precision, inexact 6 decimal digits precision

I started with the following

@IsNumber()
@Min(-90)
@Max(90)
/* precision check */
public latitude: number;

@IsNumber()
@Min(-180)
@Max(180)
/* precision check */
public longitude: number;

and would like to know how I can check for the precision of those floating numbers. It must have a 6 digits precision.

Thanks in advance

Upvotes: 1

Views: 6804

Answers (2)

José Neto
José Neto

Reputation: 507

The package already comes with validators for this kind of input

@IsLatLong()
@IsLatitude()
@IsLongitude()

Upvotes: 5

satanTime
satanTime

Reputation: 13574

For all custom stuff such as a precision the best way is to write an own custom validator where you can use a regexp or an other way to verify the input data.

The easiest way is to write a validator: https://github.com/typestack/class-validator#custom-validation-classes

More advanced is to write an own validation decorator: https://github.com/typestack/class-validator#custom-validation-decorators

Upvotes: 2

Related Questions