-
Notifications
You must be signed in to change notification settings - Fork 1
/
app-basic.ts
35 lines (31 loc) · 1.09 KB
/
app-basic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {Post} from "./types/Post";
import {Blog} from "./types/Blog";
import {DateTime} from "luxon";
import {ValidateFunction} from "ajv";
import * as validations from './types/schemas/validations';
const blog: Blog = {
site: "rehanvdm.com",
email: "[email protected]",
about: "My blog, the one I never have time to write for but do it anyway.",
posts: [{
title: "Valid Post",
rating: 5,
createAt: DateTime.now().toJSDate()
}]
};
let postInValid = {
title: "Invalid Post! Missing createAt, forcing by casting",
rating: 1
} as Post;
blog.posts.push(postInValid);
const validateBlog = validations.Blog as ValidateFunction<Blog>;
if(!validateBlog)
throw new Error("Validate not defined, schema not found");
/* Casting to and from JSON forces the object to be represented in its primitive types.
* The Date object for example will be forced to a ISO 8601 representation which is what we want */
if(!validateBlog(JSON.parse(JSON.stringify(blog))))
{
console.error(validateBlog.errors);
console.error(JSON.stringify(validateBlog.errors));
throw new Error("Blog not valid");
}