Skip to content

Commit a752cb6

Browse files
committed
feat: accept error messages for required,array, and object validations
1 parent 79acc18 commit a752cb6

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

factories/output.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import { defineInlineFunctions } from '../src/scripts/define_inline_functions.js
1818
export function getInitialOutput(options?: CompilerOptions) {
1919
return [
2020
`async function anonymous(root,meta,refs,messagesProvider,errorReporter) {`,
21-
...defineInlineErrorMessages().split(EOL),
21+
...defineInlineErrorMessages({
22+
required: 'value is required',
23+
object: 'value is not a valid object',
24+
array: 'value is not a valid array',
25+
}).split(EOL),
2226
...defineInlineFunctions(options || { convertEmptyStringsToNull: false }).split(EOL),
2327
'let out;',
2428
]

src/compiler/main.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@ export class Compiler {
7373
* Initiates the JS output
7474
*/
7575
#initiateJSOutput() {
76-
this.#buffer.writeStatement(defineInlineErrorMessages())
76+
this.#buffer.writeStatement(
77+
defineInlineErrorMessages({
78+
required: 'value is required',
79+
object: 'value is not a valid object',
80+
array: 'value is not a valid array',
81+
...this.#options.messages,
82+
})
83+
)
7784
this.#buffer.writeStatement(defineInlineFunctions(this.#options))
7885
this.#buffer.writeStatement('let out;')
7986
}

src/scripts/define_error_messages.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import { CompilerOptions } from '../types.js'
11+
1012
/**
1113
* Returns JS fragment for inline error messages for errors raised
1214
* by the compiler.
1315
*/
14-
export function defineInlineErrorMessages() {
15-
return `const REQUIRED = 'value is required';
16-
const NOT_AN_OBJECT = 'value is not a valid object';
17-
const NOT_AN_ARRAY = 'value is not a valid array';`
16+
export function defineInlineErrorMessages(
17+
messages: Required<Exclude<CompilerOptions['messages'], undefined>>
18+
) {
19+
return `const REQUIRED = '${messages.required}';
20+
const NOT_AN_OBJECT = '${messages.object}';
21+
const NOT_AN_ARRAY = '${messages.array}';`
1822
}

src/types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,14 @@ export type CompilerOptions = {
523523
* normalization
524524
*/
525525
convertEmptyStringsToNull: boolean
526+
527+
/**
528+
* Provide messages to use for required, object and
529+
* array validations.
530+
*/
531+
messages?: Partial<{
532+
required: string
533+
object: string
534+
array: string
535+
}>
526536
}

0 commit comments

Comments
 (0)