forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validate.ts
110 lines (86 loc) · 2.42 KB
/
Validate.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import path from 'node:path'
import {
arg,
Command,
format,
getConfig,
getLintWarningsAsText,
handleLintPanic,
HelpError,
lintSchema,
loadEnvFile,
logger,
validate,
} from '@prisma/internals'
import { getSchemaPathAndPrint } from '@prisma/migrate'
import { bold, dim, red, underline } from 'kleur/colors'
/**
* $ prisma validate
*/
export class Validate implements Command {
public static new(): Validate {
return new Validate()
}
private static help = format(`
Validate a Prisma schema.
${bold('Usage')}
${dim('$')} prisma validate [options]
${bold('Options')}
-h, --help Display this help message
--schema Custom path to your Prisma schema
${bold('Examples')}
With an existing Prisma schema
${dim('$')} prisma validate
Or specify a Prisma schema path
${dim('$')} prisma validate --schema=./schema.prisma
`)
public async parse(argv: string[]): Promise<string | Error> {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--schema': String,
'--telemetry-information': String,
})
if (args instanceof Error) {
return this.help(args.message)
}
if (args['--help']) {
return this.help()
}
await loadEnvFile({ schemaPath: args['--schema'], printMessage: true })
const { schemaPath, schemas } = await getSchemaPathAndPrint(args['--schema'])
const { lintDiagnostics } = handleLintPanic(
() => {
// the only possible error here is a Rust panic
const lintDiagnostics = lintSchema({ schemas })
return { lintDiagnostics }
},
{ schemas },
)
const lintWarnings = getLintWarningsAsText(lintDiagnostics)
if (lintWarnings && logger.should.warn()) {
// Output warnings to stderr
console.warn(lintWarnings)
}
validate({
schemas,
})
// We could have a CLI flag to ignore env var validation
await getConfig({
datamodel: schemas,
ignoreEnvVarErrors: false,
})
const schemaRelativePath = path.relative(process.cwd(), schemaPath)
if (schemas.length > 1) {
return `The schemas at ${underline(schemaRelativePath)} are valid 🚀`
}
return `The schema at ${underline(schemaRelativePath)} is valid 🚀`
}
// help message
public help(error?: string): string | HelpError {
if (error) {
return new HelpError(`\n${bold(red(`!`))} ${error}\n${Validate.help}`)
}
return Validate.help
}
}