Making fields/columns optional/required using TypeORM

Multi tool use
Making fields/columns optional/required using TypeORM
I have this cURL command that successfully wrote to MySQL:
curl -d '{"key1":"value", "key2":"value"}' -H "Content-Type: application/json" -X POST http://localhost:3010/customers
This query was able to write to the db through via the TypeORM library, like so:
import {Customer} from "../entity/customer";
import {getRepository} from "typeorm";
const RCustomer = getRepository(Customer);
router.post('/', (req, res, next) => {
return RCustomer.save(req.body).then(v => res.json({success: v}));
});
that never should have happened since "key1" and "key2" aren't fields in the customers table!
the customers model looks like:
'use strict';
import {Entity, PrimaryGeneratedColumn, Column, Index} from "typeorm";
import {MooveBaseEntity} from "./base";
@Entity()
@Index(["email"], { unique: true })
export class Customer extends MooveBaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
email: string;
@Column()
phonePrimary: string;
@Column()
phoneSecondary: string;
}
so what I am thinking is - I need a way to make certain fields required. Ideally all fields would be required by default, and then I could make some optional (nullable, or whatever).
How do I do this? Basically, that cURL command should never have succeeded.
2 Answers
2
Actually you can use third-party validation library that can be used to validate your model before persistence occurs: https://github.com/typestack/class-validator
The benefits - you can also use annotation to extend your current model with extra validation requirements.
So before calling "save" action you can perform validate and relying to validation result process or skip saving action.
import {validate} from "class-validator";
...
validate(customer).then(errors => { // errors is an array of validation errors
if (errors.length > 0) {
console.log("validation failed. errors: ", errors);
} else {
RCustomer.save(customer).then(v => res.json({success: v}));
}
});
where required properties can be described like:
@Entity()
@Index(["email"], { unique: true })
export class Customer extends MooveBaseEntity {
@PrimaryGeneratedColumn()
@IsDefined()
id: number;
@Column()
@IsDefined()
firstName: string;
...
}
I am surprised that TypeORM cannot do basic validation out of the box. Surely the db won't allow you to write a table with extraneous columns?
– Olegzandr Denman
Jul 2 at 8:21
I think it depends on which provider are you using - because with having mongoDB provide you can write dynamic document. So you will not have single behaviors across all providers. Maybe this is the reason that validation was not part of library.
– VadimB
Jul 2 at 8:24
yeah that could be it
– Olegzandr Denman
Jul 2 at 8:52
You have such behaviour because save
does not care about properties in the object you sent to it except for the properties it needs. In your case you did not sent any properties TypeORM needs, so for typeorm your object is basically {}
. save({})
is valid in your case since all columns in your aren't required. To make them required you need to explicitly change their nullable state, e.g.:
save
{}
save({})
@Column({ nullable: false })
firstName: string;
This makes sense, thanks for your answer
– Alexander Mills
Jul 2 at 20:13
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
thanks, interesting, why is this separate from typeorm?
– Olegzandr Denman
Jul 2 at 8:18