forked from firebase/firebase-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebaseConfigValidate.ts
44 lines (39 loc) · 1.43 KB
/
firebaseConfigValidate.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
36
37
38
39
40
41
42
43
44
// Note: Upgraded ajv from 6 to 8 as we upgraded from Typescript 3
import { ValidateFunction, ErrorObject } from "ajv";
import * as fs from "fs";
import * as path from "path";
import { Ajv } from "ajv";
import addFormats from "ajv-formats";
// We need to allow union types becuase typescript-json-schema generates them sometimes.
const ajv = new Ajv({ allowUnionTypes: true });
addFormats(ajv);
let _VALIDATOR: ValidateFunction | undefined = undefined;
/**
* Lazily load the 'schema/firebase-config.json' file and return an AJV validation
* function. By doing this lazily we don't impose this I/O cost on those using
* the CLI as a Node module.
*/
export function getValidator(): ValidateFunction {
if (!_VALIDATOR) {
const schemaStr = fs.readFileSync(
path.resolve(__dirname, "../schema/firebase-config.json"),
"utf-8",
);
const schema = JSON.parse(schemaStr);
_VALIDATOR = ajv.compile(schema);
}
return _VALIDATOR!;
}
export function getErrorMessage(e: ErrorObject) {
if (e.keyword === "additionalProperties") {
return `Object "${e.instancePath}" in "firebase.json" has unknown property: ${JSON.stringify(
e.params,
)}`;
} else if (e.keyword === "required") {
return `Object "${
e.instancePath
}" in "firebase.json" is missing required property: ${JSON.stringify(e.params)}`;
} else {
return `Field "${e.instancePath}" in "firebase.json" is possibly invalid: ${e.message}`;
}
}