@vinejs/compiler
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -0,2 +1,10 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export { Compiler } from './src/compiler/main.js'; | ||
export { refsBuilder } from './src/refs_builder.js'; |
@@ -0,8 +1,26 @@ | ||
/** | ||
* Compiler buffer to collect JS fragments in memory | ||
*/ | ||
export declare class CompilerBuffer { | ||
#private; | ||
/** | ||
* The character used to create a new line | ||
*/ | ||
newLine: string; | ||
/** | ||
* Write statement ot the output | ||
*/ | ||
writeStatement(statement: string): void; | ||
/** | ||
* Creates a child buffer | ||
*/ | ||
child(): CompilerBuffer; | ||
/** | ||
* Returns the buffer contents as string | ||
*/ | ||
toString(): string; | ||
/** | ||
* Flush in-memory string | ||
*/ | ||
flush(): void; | ||
} |
@@ -0,13 +1,39 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Compiler buffer to collect JS fragments in memory | ||
*/ | ||
export class CompilerBuffer { | ||
#content = ''; | ||
/** | ||
* The character used to create a new line | ||
*/ | ||
newLine = '\n'; | ||
/** | ||
* Write statement ot the output | ||
*/ | ||
writeStatement(statement) { | ||
this.#content = `${this.#content}${this.newLine}${statement}`; | ||
} | ||
/** | ||
* Creates a child buffer | ||
*/ | ||
child() { | ||
return new CompilerBuffer(); | ||
} | ||
/** | ||
* Returns the buffer contents as string | ||
*/ | ||
toString() { | ||
return this.#content; | ||
} | ||
/** | ||
* Flush in-memory string | ||
*/ | ||
flush() { | ||
@@ -14,0 +40,0 @@ this.#content = ''; |
@@ -0,2 +1,17 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export function createArrayField(parent) { | ||
/** | ||
* Commented to see if a use case arrives for using this. | ||
*/ | ||
// const fieldPathExpression = | ||
// parent.fieldPathExpression !== `''` | ||
// ? `${parent.fieldPathExpression} + '.' + ${parent.variableName}_i` | ||
// : `${parent.variableName}_i` | ||
const wildCardPath = parent.wildCardPath !== '' ? `${parent.wildCardPath}.*` : `*`; | ||
@@ -3,0 +18,0 @@ return { |
@@ -0,2 +1,17 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export function createObjectField(node, variablesCounter, parent) { | ||
/** | ||
* Commented to see if a use case arrives for using this. | ||
*/ | ||
// const fieldPathExpression = | ||
// parent.fieldPathExpression !== `''` | ||
// ? `${parent.fieldPathExpression} + '.' + '${node.fieldName}'` | ||
// : `'${node.fieldName}'` | ||
const wildCardPath = parent.wildCardPath !== '' ? `${parent.wildCardPath}.${node.fieldName}` : node.fieldName; | ||
@@ -3,0 +18,0 @@ return { |
@@ -0,2 +1,17 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export function createRecordField(parent) { | ||
/** | ||
* Commented to see if a use case arrives for using this. | ||
*/ | ||
// const fieldPathExpression = | ||
// parent.fieldPathExpression !== `''` | ||
// ? `${parent.fieldPathExpression} + '.' + ${parent.variableName}_i` | ||
// : `${parent.variableName}_i` | ||
const wildCardPath = parent.wildCardPath !== '' ? `${parent.wildCardPath}.*` : `*`; | ||
@@ -3,0 +18,0 @@ return { |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export function createRootField(parent) { | ||
@@ -2,0 +10,0 @@ return { |
@@ -0,2 +1,17 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export function createTupleField(node, parent) { | ||
/** | ||
* Commented to see if a use case arrives for using this. | ||
*/ | ||
// const fieldPathExpression = | ||
// parent.fieldPathExpression !== `''` | ||
// ? `${parent.fieldPathExpression} + '.' + '${node.fieldName}'` | ||
// : `'${node.fieldName}'` | ||
const wildCardPath = parent.wildCardPath !== '' ? `${parent.wildCardPath}.${node.fieldName}` : node.fieldName; | ||
@@ -3,0 +18,0 @@ return { |
import { CompilerBuffer } from './buffer.js'; | ||
import type { Refs, RootNode, CompilerField, CompilerNodes, CompilerParent, CompilerOptions, ErrorReporterContract, MessagesProviderContact } from '../types.js'; | ||
/** | ||
* Compiler is used to compile an array of schema nodes into a re-usable | ||
* JavaScript. | ||
*/ | ||
export declare class Compiler { | ||
#private; | ||
/** | ||
* Variables counter is used to generate unique variable | ||
* names with a counter suffix. | ||
*/ | ||
variablesCounter: number; | ||
constructor(rootNode: RootNode, options?: CompilerOptions); | ||
/** | ||
* Converts a node to a field. Optionally accepts a parent node to create | ||
* a field for a specific parent type. | ||
*/ | ||
createFieldFor(node: CompilerNodes, parent: CompilerParent): CompilerField; | ||
/** | ||
* Compiles a given compiler node | ||
*/ | ||
compileNode(node: CompilerNodes, buffer: CompilerBuffer, parent: CompilerParent, parentField?: CompilerField): void; | ||
/** | ||
* Compile schema nodes to an async function | ||
*/ | ||
compile(): (data: any, meta: Record<string, any>, refs: Refs, messagesProvider: MessagesProviderContact, errorReporter: ErrorReporterContract) => Promise<Record<string, any>>; | ||
} |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { CompilerBuffer } from './buffer.js'; | ||
@@ -16,7 +24,27 @@ import { TupleNodeCompiler } from './nodes/tuple.js'; | ||
import { defineInlineErrorMessages } from '../scripts/define_error_messages.js'; | ||
/** | ||
* Representation of an async function | ||
*/ | ||
const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor; | ||
/** | ||
* Compiler is used to compile an array of schema nodes into a re-usable | ||
* JavaScript. | ||
*/ | ||
export class Compiler { | ||
/** | ||
* Variables counter is used to generate unique variable | ||
* names with a counter suffix. | ||
*/ | ||
variablesCounter = 0; | ||
/** | ||
* An array of nodes to process | ||
*/ | ||
#rootNode; | ||
/** | ||
* Options to configure the compiler behavior | ||
*/ | ||
#options; | ||
/** | ||
* Buffer for collection the JS output string | ||
*/ | ||
#buffer = new CompilerBuffer(); | ||
@@ -27,2 +55,5 @@ constructor(rootNode, options) { | ||
} | ||
/** | ||
* Initiates the JS output | ||
*/ | ||
#initiateJSOutput() { | ||
@@ -38,2 +69,5 @@ this.#buffer.writeStatement(defineInlineErrorMessages({ | ||
} | ||
/** | ||
* Finished the JS output | ||
*/ | ||
#finishJSOutput() { | ||
@@ -43,2 +77,5 @@ this.#buffer.writeStatement(reportErrors()); | ||
} | ||
/** | ||
* Compiles all the nodes | ||
*/ | ||
#compileNodes() { | ||
@@ -53,5 +90,12 @@ this.compileNode(this.#rootNode.schema, this.#buffer, { | ||
} | ||
/** | ||
* Returns compiled output as a function | ||
*/ | ||
#toAsyncFunction() { | ||
return new AsyncFunction('root', 'meta', 'refs', 'messagesProvider', 'errorReporter', this.#buffer.toString()); | ||
} | ||
/** | ||
* Converts a node to a field. Optionally accepts a parent node to create | ||
* a field for a specific parent type. | ||
*/ | ||
createFieldFor(node, parent) { | ||
@@ -71,2 +115,5 @@ switch (parent.type) { | ||
} | ||
/** | ||
* Compiles a given compiler node | ||
*/ | ||
compileNode(node, buffer, parent, parentField) { | ||
@@ -88,2 +135,5 @@ switch (node.type) { | ||
} | ||
/** | ||
* Compile schema nodes to an async function | ||
*/ | ||
compile() { | ||
@@ -90,0 +140,0 @@ this.#initiateJSOutput(); |
@@ -5,2 +5,5 @@ import { BaseNode } from './base.js'; | ||
import type { CompilerField, CompilerParent, ArrayNode } from '../../types.js'; | ||
/** | ||
* Compiles an array schema node to JS string output. | ||
*/ | ||
export declare class ArrayNodeCompiler extends BaseNode { | ||
@@ -7,0 +10,0 @@ #private; |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { BaseNode } from './base.js'; | ||
@@ -9,2 +17,5 @@ import { defineArrayLoop } from '../../scripts/array/loop.js'; | ||
import { defineFieldExistenceValidations } from '../../scripts/field/existence_validations.js'; | ||
/** | ||
* Compiles an array schema node to JS string output. | ||
*/ | ||
export class ArrayNodeCompiler extends BaseNode { | ||
@@ -20,2 +31,5 @@ #node; | ||
} | ||
/** | ||
* Compiles the array elements to a JS fragment | ||
*/ | ||
#compileArrayElements() { | ||
@@ -40,3 +54,9 @@ const arrayElementsBuffer = this.#buffer.child(); | ||
compile() { | ||
/** | ||
* Define 1: Define field variable | ||
*/ | ||
this.defineField(this.#buffer); | ||
/** | ||
* Step 2: Define code to validate the existence of field. | ||
*/ | ||
this.#buffer.writeStatement(defineFieldExistenceValidations({ | ||
@@ -47,2 +67,8 @@ allowNull: this.#node.allowNull, | ||
})); | ||
/** | ||
* Wrapping initialization of output + array elements | ||
* validation inside `if array field is valid` block. | ||
* | ||
* Pre step: 3 | ||
*/ | ||
const isArrayValidBlock = defineIsValidGuard({ | ||
@@ -57,2 +83,8 @@ variableName: this.field.variableName, | ||
}); | ||
/** | ||
* Wrapping field validations + "isArrayValidBlock" inside | ||
* `if value is array` check. | ||
* | ||
* Pre step: 3 | ||
*/ | ||
const isValueAnArrayBlock = defineArrayGuard({ | ||
@@ -67,2 +99,6 @@ variableName: this.field.variableName, | ||
}); | ||
/** | ||
* Step 3: Define `if value is an array` block and `else if value is null` | ||
* block. | ||
*/ | ||
this.#buffer.writeStatement(`${isValueAnArrayBlock}${this.#buffer.newLine}${defineFieldNullOutput({ | ||
@@ -69,0 +105,0 @@ allowNull: this.#node.allowNull, |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { defineFieldVariables } from '../../scripts/field/variables.js'; | ||
@@ -2,0 +10,0 @@ export class BaseNode { |
@@ -5,2 +5,5 @@ import { BaseNode } from './base.js'; | ||
import type { LiteralNode, CompilerParent, CompilerField } from '../../types.js'; | ||
/** | ||
* Compiles a literal schema node to JS string output. | ||
*/ | ||
export declare class LiteralNodeCompiler extends BaseNode { | ||
@@ -7,0 +10,0 @@ #private; |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { BaseNode } from './base.js'; | ||
@@ -6,2 +14,5 @@ import { defineFieldValidations } from '../../scripts/field/validations.js'; | ||
import { defineFieldExistenceValidations } from '../../scripts/field/existence_validations.js'; | ||
/** | ||
* Compiles a literal schema node to JS string output. | ||
*/ | ||
export class LiteralNodeCompiler extends BaseNode { | ||
@@ -16,3 +27,9 @@ #node; | ||
compile() { | ||
/** | ||
* Define 1: Define field variable | ||
*/ | ||
this.defineField(this.#buffer); | ||
/** | ||
* Step 2: Define block to validate the existence of field | ||
*/ | ||
this.#buffer.writeStatement(defineFieldExistenceValidations({ | ||
@@ -23,2 +40,5 @@ allowNull: this.#node.allowNull, | ||
})); | ||
/** | ||
* Step 3: Define code to run validations on field | ||
*/ | ||
this.#buffer.writeStatement(defineFieldValidations({ | ||
@@ -30,2 +50,5 @@ variableName: this.field.variableName, | ||
})); | ||
/** | ||
* Step 4: Define block to save the output value or the null value | ||
*/ | ||
this.#buffer.writeStatement(`${defineFieldValueOutput({ | ||
@@ -32,0 +55,0 @@ variableName: this.field.variableName, |
@@ -5,2 +5,5 @@ import { BaseNode } from './base.js'; | ||
import type { CompilerField, CompilerParent, ObjectNode } from '../../types.js'; | ||
/** | ||
* Compiles an object schema node to JS string output. | ||
*/ | ||
export declare class ObjectNodeCompiler extends BaseNode { | ||
@@ -7,0 +10,0 @@ #private; |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { BaseNode } from './base.js'; | ||
@@ -11,2 +19,5 @@ import { defineObjectGuard } from '../../scripts/object/guard.js'; | ||
import { defineFieldExistenceValidations } from '../../scripts/field/existence_validations.js'; | ||
/** | ||
* Compiles an object schema node to JS string output. | ||
*/ | ||
export class ObjectNodeCompiler extends BaseNode { | ||
@@ -22,2 +33,5 @@ #node; | ||
} | ||
/** | ||
* Returns known field names for the object | ||
*/ | ||
#getFieldNames(node) { | ||
@@ -28,2 +42,5 @@ let fieldNames = node.properties.map((child) => child.fieldName); | ||
} | ||
/** | ||
* Returns field names of a group. | ||
*/ | ||
#getGroupFieldNames(group) { | ||
@@ -34,2 +51,5 @@ return group.conditions.flatMap((condition) => { | ||
} | ||
/** | ||
* Compiles object children to JS output | ||
*/ | ||
#compileObjectChildren() { | ||
@@ -47,2 +67,5 @@ const buffer = this.#buffer.child(); | ||
} | ||
/** | ||
* Compiles object groups with conditions to JS output. | ||
*/ | ||
#compileObjectGroups() { | ||
@@ -60,2 +83,5 @@ const buffer = this.#buffer.child(); | ||
} | ||
/** | ||
* Compiles an object groups recursively | ||
*/ | ||
#compileObjectGroup(group, buffer, parent) { | ||
@@ -77,2 +103,5 @@ group.conditions.forEach((condition, index) => { | ||
}); | ||
/** | ||
* Define else block | ||
*/ | ||
if (group.elseConditionalFnRefId && group.conditions.length) { | ||
@@ -86,3 +115,9 @@ buffer.writeStatement(defineElseCondition({ | ||
compile() { | ||
/** | ||
* Define 1: Define field variable | ||
*/ | ||
this.defineField(this.#buffer); | ||
/** | ||
* Step 2: Define code to validate the existence of field. | ||
*/ | ||
this.#buffer.writeStatement(defineFieldExistenceValidations({ | ||
@@ -93,2 +128,8 @@ allowNull: this.#node.allowNull, | ||
})); | ||
/** | ||
* Wrapping initialization of output + object children validations | ||
* validation inside `if object field is valid` block. | ||
* | ||
* Pre step: 3 | ||
*/ | ||
const isObjectValidBlock = defineIsValidGuard({ | ||
@@ -107,2 +148,8 @@ variableName: this.field.variableName, | ||
}); | ||
/** | ||
* Wrapping field validations + "isObjectValidBlock" inside | ||
* `if value is object` check. | ||
* | ||
* Pre step: 3 | ||
*/ | ||
const isValueAnObject = defineObjectGuard({ | ||
@@ -117,2 +164,6 @@ variableName: this.field.variableName, | ||
}); | ||
/** | ||
* Step 3: Define `if value is an object` block and `else if value is null` | ||
* block. | ||
*/ | ||
this.#buffer.writeStatement(`${isValueAnObject}${this.#buffer.newLine}${defineFieldNullOutput({ | ||
@@ -119,0 +170,0 @@ variableName: this.field.variableName, |
@@ -5,2 +5,5 @@ import { BaseNode } from './base.js'; | ||
import type { CompilerField, CompilerParent, RecordNode } from '../../types.js'; | ||
/** | ||
* Compiles a record schema node to JS string output. | ||
*/ | ||
export declare class RecordNodeCompiler extends BaseNode { | ||
@@ -7,0 +10,0 @@ #private; |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { BaseNode } from './base.js'; | ||
@@ -9,2 +17,5 @@ import { defineRecordLoop } from '../../scripts/record/loop.js'; | ||
import { defineFieldExistenceValidations } from '../../scripts/field/existence_validations.js'; | ||
/** | ||
* Compiles a record schema node to JS string output. | ||
*/ | ||
export class RecordNodeCompiler extends BaseNode { | ||
@@ -20,2 +31,5 @@ #node; | ||
} | ||
/** | ||
* Compiles the record elements to a JS fragment | ||
*/ | ||
#compileRecordElements() { | ||
@@ -39,3 +53,9 @@ const buffer = this.#buffer.child(); | ||
compile() { | ||
/** | ||
* Define 1: Define field variable | ||
*/ | ||
this.defineField(this.#buffer); | ||
/** | ||
* Step 2: Define code to validate the existence of field. | ||
*/ | ||
this.#buffer.writeStatement(defineFieldExistenceValidations({ | ||
@@ -46,2 +66,8 @@ allowNull: this.#node.allowNull, | ||
})); | ||
/** | ||
* Wrapping initialization of output + tuple validation + array elements | ||
* validation inside `if array field is valid` block. | ||
* | ||
* Pre step: 3 | ||
*/ | ||
const isObjectValidBlock = defineIsValidGuard({ | ||
@@ -56,2 +82,8 @@ variableName: this.field.variableName, | ||
}); | ||
/** | ||
* Wrapping field validations + "isArrayValidBlock" inside | ||
* `if value is array` check. | ||
* | ||
* Pre step: 3 | ||
*/ | ||
const isValueAnObjectBlock = defineObjectGuard({ | ||
@@ -66,2 +98,6 @@ variableName: this.field.variableName, | ||
}); | ||
/** | ||
* Step 3: Define `if value is an object` block and `else if value is null` | ||
* block. | ||
*/ | ||
this.#buffer.writeStatement(`${isValueAnObjectBlock}${this.#buffer.newLine}${defineFieldNullOutput({ | ||
@@ -68,0 +104,0 @@ allowNull: this.#node.allowNull, |
@@ -5,2 +5,5 @@ import { BaseNode } from './base.js'; | ||
import type { CompilerField, CompilerParent, TupleNode } from '../../types.js'; | ||
/** | ||
* Compiles a tuple schema node to JS string output. | ||
*/ | ||
export declare class TupleNodeCompiler extends BaseNode { | ||
@@ -7,0 +10,0 @@ #private; |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { BaseNode } from './base.js'; | ||
@@ -8,2 +16,5 @@ import { defineArrayGuard } from '../../scripts/array/guard.js'; | ||
import { defineFieldExistenceValidations } from '../../scripts/field/existence_validations.js'; | ||
/** | ||
* Compiles a tuple schema node to JS string output. | ||
*/ | ||
export class TupleNodeCompiler extends BaseNode { | ||
@@ -19,2 +30,5 @@ #node; | ||
} | ||
/** | ||
* Compiles the tuple children to a JS fragment | ||
*/ | ||
#compileTupleChildren() { | ||
@@ -35,3 +49,9 @@ const buffer = this.#buffer.child(); | ||
compile() { | ||
/** | ||
* Define 1: Define field variable | ||
*/ | ||
this.defineField(this.#buffer); | ||
/** | ||
* Step 2: Define code to validate the existence of field. | ||
*/ | ||
this.#buffer.writeStatement(defineFieldExistenceValidations({ | ||
@@ -42,2 +62,8 @@ allowNull: this.#node.allowNull, | ||
})); | ||
/** | ||
* Wrapping initialization of output + tuple validation | ||
* validation inside `if array field is valid` block. | ||
* | ||
* Pre step: 3 | ||
*/ | ||
const isArrayValidBlock = defineIsValidGuard({ | ||
@@ -54,2 +80,8 @@ variableName: this.field.variableName, | ||
}); | ||
/** | ||
* Wrapping field validations + "isArrayValidBlock" inside | ||
* `if value is array` check. | ||
* | ||
* Pre step: 3 | ||
*/ | ||
const isValueAnArrayBlock = defineArrayGuard({ | ||
@@ -64,2 +96,6 @@ variableName: this.field.variableName, | ||
}); | ||
/** | ||
* Step 3: Define `if value is an array` block and `else if value is null` | ||
* block. | ||
*/ | ||
this.#buffer.writeStatement(`${isValueAnArrayBlock}${this.#buffer.newLine}${defineFieldNullOutput({ | ||
@@ -66,0 +102,0 @@ allowNull: this.#node.allowNull, |
@@ -5,2 +5,5 @@ import { BaseNode } from './base.js'; | ||
import type { CompilerField, CompilerParent, UnionNode } from '../../types.js'; | ||
/** | ||
* Compiles a union schema node to JS string output. | ||
*/ | ||
export declare class UnionNodeCompiler extends BaseNode { | ||
@@ -7,0 +10,0 @@ #private; |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { BaseNode } from './base.js'; | ||
@@ -5,2 +13,5 @@ import { callParseFunction } from '../../scripts/union/parse.js'; | ||
import { defineConditionalGuard } from '../../scripts/define_conditional_guard.js'; | ||
/** | ||
* Compiles a union schema node to JS string output. | ||
*/ | ||
export class UnionNodeCompiler extends BaseNode { | ||
@@ -18,2 +29,6 @@ #compiler; | ||
} | ||
/** | ||
* Compiles union children by wrapping each conditon inside a conditional | ||
* guard block | ||
*/ | ||
#compileUnionChildren() { | ||
@@ -23,2 +38,5 @@ const childrenBuffer = this.#buffer.child(); | ||
const conditionalBuffer = this.#buffer.child(); | ||
/** | ||
* Parse the value once the condition is true | ||
*/ | ||
if ('parseFnId' in child.schema) { | ||
@@ -39,2 +57,5 @@ conditionalBuffer.writeStatement(callParseFunction({ | ||
}); | ||
/** | ||
* Define else block | ||
*/ | ||
if (this.#node.elseConditionalFnRefId && this.#node.conditions.length) { | ||
@@ -49,5 +70,12 @@ childrenBuffer.writeStatement(defineElseCondition({ | ||
compile() { | ||
/** | ||
* Define 1: Define field variable | ||
*/ | ||
this.defineField(this.#buffer); | ||
/** | ||
* Step 2: Compile union children wrapped inside predicate | ||
* condition. | ||
*/ | ||
this.#buffer.writeStatement(this.#compileUnionChildren()); | ||
} | ||
} |
import type { RefsStore } from './types.js'; | ||
/** | ||
* Creates a refs store for parsing the schema | ||
*/ | ||
export declare function refsBuilder(): RefsStore; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Creates a refs store for parsing the schema | ||
*/ | ||
export function refsBuilder() { | ||
@@ -8,2 +19,5 @@ let counter = 0; | ||
}, | ||
/** | ||
* Track a value inside refs | ||
*/ | ||
track(value) { | ||
@@ -15,11 +29,23 @@ counter++; | ||
}, | ||
/** | ||
* Track a validation inside refs | ||
*/ | ||
trackValidation(validation) { | ||
return this.track(validation); | ||
}, | ||
/** | ||
* Track input value parser inside refs | ||
*/ | ||
trackParser(fn) { | ||
return this.track(fn); | ||
}, | ||
/** | ||
* Track output value transformer inside refs | ||
*/ | ||
trackTransformer(fn) { | ||
return this.track(fn); | ||
}, | ||
/** | ||
* Track a conditional inside refs | ||
*/ | ||
trackConditional(fn) { | ||
@@ -26,0 +52,0 @@ return this.track(fn); |
@@ -5,3 +5,6 @@ type ArrayGuardOptions = { | ||
}; | ||
/** | ||
* Returns JS fragment to wrap code inside an array conditional | ||
*/ | ||
export declare function defineArrayGuard({ variableName, guardedCodeSnippet }: ArrayGuardOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment to wrap code inside an array conditional | ||
*/ | ||
export function defineArrayGuard({ variableName, guardedCodeSnippet }) { | ||
@@ -2,0 +13,0 @@ return `if (ensureIsArray(${variableName})) { |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Options accepts by the output script | ||
*/ | ||
type OutputOptions = { | ||
@@ -6,3 +9,6 @@ variableName: string; | ||
}; | ||
/** | ||
* Returns JS fragment for writing the initial output for an array | ||
*/ | ||
export declare function defineArrayInitialOutput({ variableName, outputExpression, outputValueExpression, }: OutputOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for writing the initial output for an array | ||
*/ | ||
export function defineArrayInitialOutput({ variableName, outputExpression, outputValueExpression, }) { | ||
@@ -2,0 +13,0 @@ return `const ${variableName}_out = ${outputValueExpression}; |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Options accepts by the loop script | ||
*/ | ||
type ArrayLoopOptions = { | ||
@@ -6,3 +9,6 @@ variableName: string; | ||
}; | ||
/** | ||
* Returns JS fragment for wrapping code inside an array loop | ||
*/ | ||
export declare function defineArrayLoop({ variableName, loopCodeSnippet, startingIndex, }: ArrayLoopOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for wrapping code inside an array loop | ||
*/ | ||
export function defineArrayLoop({ variableName, loopCodeSnippet, startingIndex, }) { | ||
@@ -2,0 +13,0 @@ startingIndex = startingIndex || 0; |
@@ -7,3 +7,6 @@ type ConditionalGuardOptions = { | ||
}; | ||
/** | ||
* Returns JS fragment to wrap code inside a conditional guard | ||
*/ | ||
export declare function defineConditionalGuard({ conditional, variableName, conditionalFnRefId, guardedCodeSnippet, }: ConditionalGuardOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment to wrap code inside a conditional guard | ||
*/ | ||
export function defineConditionalGuard({ conditional, variableName, conditionalFnRefId, guardedCodeSnippet, }) { | ||
@@ -2,0 +13,0 @@ return `${conditional}(refs['${conditionalFnRefId}'](${variableName}.value, ${variableName})) { |
@@ -5,3 +5,6 @@ type ConditionalGuardOptions = { | ||
}; | ||
/** | ||
* Returns JS fragment to invoke a function inside else block | ||
*/ | ||
export declare function defineElseCondition({ variableName, conditionalFnRefId }: ConditionalGuardOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment to invoke a function inside else block | ||
*/ | ||
export function defineElseCondition({ variableName, conditionalFnRefId }) { | ||
@@ -2,0 +13,0 @@ return `else { |
import { CompilerOptions } from '../types.js'; | ||
/** | ||
* Returns JS fragment for inline error messages for errors raised | ||
* by the compiler. | ||
*/ | ||
export declare function defineInlineErrorMessages(messages: Required<Exclude<CompilerOptions['messages'], undefined>>): string; |
@@ -0,1 +1,13 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for inline error messages for errors raised | ||
* by the compiler. | ||
*/ | ||
export function defineInlineErrorMessages(messages) { | ||
@@ -2,0 +14,0 @@ return `const REQUIRED = '${messages.required}'; |
@@ -0,3 +1,7 @@ | ||
/** | ||
* Returns JS fragment for inline function needed by the | ||
* validation runtime code. | ||
*/ | ||
export declare function defineInlineFunctions(options: { | ||
convertEmptyStringsToNull: boolean; | ||
}): string; |
@@ -0,1 +1,13 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for inline function needed by the | ||
* validation runtime code. | ||
*/ | ||
export function defineInlineFunctions(options) { | ||
@@ -2,0 +14,0 @@ return `function report(message, rule, field, args) { |
@@ -6,3 +6,6 @@ type FieldOptions = { | ||
}; | ||
/** | ||
* Returns JS fragment to validate a field's value for existence. | ||
*/ | ||
export declare function defineFieldExistenceValidations({ allowNull, isOptional, variableName, }: FieldOptions): string; | ||
export {}; |
@@ -0,3 +1,22 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment to validate a field's value for existence. | ||
*/ | ||
export function defineFieldExistenceValidations({ allowNull, isOptional, variableName, }) { | ||
/** | ||
* Validations are only performed when `isOptional` flag | ||
* is disabled. | ||
*/ | ||
if (isOptional === false) { | ||
/** | ||
* When `allowNull` flag is disabled, we should ensure the value | ||
* is not null and neither undefined. | ||
*/ | ||
if (allowNull === false) { | ||
@@ -7,2 +26,5 @@ return `ensureExists(${variableName});`; | ||
else { | ||
/** | ||
* Otherwise ensure the value is not undefined. | ||
*/ | ||
return `ensureIsDefined(${variableName});`; | ||
@@ -9,0 +31,0 @@ } |
@@ -6,3 +6,6 @@ type ObjectGuardOptions = { | ||
}; | ||
/** | ||
* Returns JS fragment to wrap code inside a valid guard | ||
*/ | ||
export declare function defineIsValidGuard({ variableName, bail, guardedCodeSnippet }: ObjectGuardOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment to wrap code inside a valid guard | ||
*/ | ||
export function defineIsValidGuard({ variableName, bail, guardedCodeSnippet }) { | ||
@@ -2,0 +13,0 @@ if (!bail) { |
import { RefIdentifier } from '../../types.js'; | ||
/** | ||
* Options accepts by the output script | ||
*/ | ||
type OutputOptions = { | ||
@@ -9,3 +12,6 @@ outputExpression: string; | ||
}; | ||
/** | ||
* Returns JS fragment for writing the null value to the output. | ||
*/ | ||
export declare function defineFieldNullOutput({ allowNull, conditional, variableName, outputExpression, transformFnRefId, }: OutputOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for writing the null value to the output. | ||
*/ | ||
export function defineFieldNullOutput({ allowNull, conditional, variableName, outputExpression, transformFnRefId, }) { | ||
@@ -2,0 +13,0 @@ if (!allowNull) { |
import { ValidationNode } from '../../types.js'; | ||
/** | ||
* Options accepts by the validation script | ||
*/ | ||
type ValidationOptions = { | ||
@@ -6,5 +9,12 @@ bail: boolean; | ||
validations: ValidationNode[]; | ||
/** | ||
* Drop missing conditional check regardless of whether | ||
* rule is implicit or not | ||
*/ | ||
dropMissingCheck: boolean; | ||
}; | ||
/** | ||
* Returns JS fragment for executing validations for a given field. | ||
*/ | ||
export declare function defineFieldValidations({ bail, validations, variableName, dropMissingCheck, }: ValidationOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Helper to generate a conditional based upon enabled conditions. | ||
*/ | ||
function wrapInConditional(conditions, wrappingCode) { | ||
@@ -20,9 +31,25 @@ const [first, second] = conditions; | ||
} | ||
/** | ||
* Emits code for executing a validation function | ||
*/ | ||
function emitValidationSnippet({ isAsync, implicit, ruleFnId }, variableName, bail, dropMissingCheck) { | ||
const rule = `refs['${ruleFnId}']`; | ||
const callable = `${rule}.validator(${variableName}.value, ${rule}.options, ${variableName});`; | ||
/** | ||
* Add "isValid" condition when the bail flag is turned on. | ||
*/ | ||
const bailCondition = bail ? `${variableName}.isValid` : ''; | ||
/** | ||
* Add the "!is_[variableName]_missing" conditional when the rule is not implicit. | ||
*/ | ||
const implicitCondition = implicit || dropMissingCheck ? '' : `${variableName}.isDefined`; | ||
/** | ||
* Wrapping the validation invocation inside conditionals based upon | ||
* enabled flags. | ||
*/ | ||
return wrapInConditional([bailCondition, implicitCondition], isAsync ? `await ${callable}` : `${callable}`); | ||
} | ||
/** | ||
* Returns JS fragment for executing validations for a given field. | ||
*/ | ||
export function defineFieldValidations({ bail, validations, variableName, dropMissingCheck, }) { | ||
@@ -29,0 +56,0 @@ return `${validations |
import { RefIdentifier } from '../../types.js'; | ||
/** | ||
* Options accepts by the output script | ||
*/ | ||
type OutputOptions = { | ||
@@ -7,3 +10,6 @@ outputExpression: string; | ||
}; | ||
/** | ||
* Returns JS fragment for writing the validated value to the output. | ||
*/ | ||
export declare function defineFieldValueOutput({ variableName, outputExpression, transformFnRefId, }: OutputOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for writing the validated value to the output. | ||
*/ | ||
export function defineFieldValueOutput({ variableName, outputExpression, transformFnRefId, }) { | ||
@@ -2,0 +13,0 @@ const outputValueExpression = transformFnRefId |
@@ -11,3 +11,8 @@ import { RefIdentifier } from '../../types.js'; | ||
}; | ||
/** | ||
* Returns JS fragment for defining the field variables. It includes, the field | ||
* value variable, context variable, and a boolean to know if the field | ||
* exists. | ||
*/ | ||
export declare function defineFieldVariables({ parseFnRefId, variableName, wildCardPath, isArrayMember, valueExpression, fieldNameExpression, parentValueExpression, }: FieldOptions): string; | ||
export {}; |
@@ -0,1 +1,14 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for defining the field variables. It includes, the field | ||
* value variable, context variable, and a boolean to know if the field | ||
* exists. | ||
*/ | ||
export function defineFieldVariables({ parseFnRefId, variableName, wildCardPath, isArrayMember, valueExpression, fieldNameExpression, parentValueExpression, }) { | ||
@@ -2,0 +15,0 @@ const inValueExpression = parseFnRefId |
@@ -5,3 +5,6 @@ type ObjectGuardOptions = { | ||
}; | ||
/** | ||
* Returns JS fragment to wrap code inside an object conditional | ||
*/ | ||
export declare function defineObjectGuard({ variableName, guardedCodeSnippet }: ObjectGuardOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment to wrap code inside an object conditional | ||
*/ | ||
export function defineObjectGuard({ variableName, guardedCodeSnippet }) { | ||
@@ -2,0 +13,0 @@ return `if (ensureIsObject(${variableName})) { |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Options accepts by the output script | ||
*/ | ||
type OutputOptions = { | ||
@@ -6,3 +9,6 @@ variableName: string; | ||
}; | ||
/** | ||
* Returns JS fragment for writing the initial output for an object | ||
*/ | ||
export declare function defineObjectInitialOutput({ variableName, outputExpression, outputValueExpression, }: OutputOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for writing the initial output for an object | ||
*/ | ||
export function defineObjectInitialOutput({ variableName, outputExpression, outputValueExpression, }) { | ||
@@ -2,0 +13,0 @@ return `const ${variableName}_out = ${outputValueExpression}; |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Options accepts by the output script | ||
*/ | ||
type MovePropertiesOptions = { | ||
@@ -6,3 +9,7 @@ variableName: string; | ||
}; | ||
/** | ||
* Returns JS fragment for moving properties from the source | ||
* to destination | ||
*/ | ||
export declare function defineMoveProperties({ variableName, fieldsToIgnore, allowUnknownProperties, }: MovePropertiesOptions): string; | ||
export {}; |
@@ -0,2 +1,14 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { inspect } from 'node:util'; | ||
/** | ||
* Returns JS fragment for moving properties from the source | ||
* to destination | ||
*/ | ||
export function defineMoveProperties({ variableName, fieldsToIgnore, allowUnknownProperties, }) { | ||
@@ -3,0 +15,0 @@ if (!allowUnknownProperties) { |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Options accepts by the loop script | ||
*/ | ||
type RecordLoopOptions = { | ||
@@ -5,3 +8,6 @@ variableName: string; | ||
}; | ||
/** | ||
* Returns JS fragment for wrapping code inside an record loop | ||
*/ | ||
export declare function defineRecordLoop({ variableName, loopCodeSnippet }: RecordLoopOptions): string; | ||
export {}; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment for wrapping code inside an record loop | ||
*/ | ||
export function defineRecordLoop({ variableName, loopCodeSnippet }) { | ||
@@ -2,0 +13,0 @@ return `const ${variableName}_keys = Object.keys(${variableName}.value); |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Returns JS fragment to report errors | ||
*/ | ||
export declare function reportErrors(): string; |
@@ -0,1 +1,12 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment to report errors | ||
*/ | ||
export function reportErrors() { | ||
@@ -2,0 +13,0 @@ return `if(errorReporter.hasErrors) { |
@@ -6,3 +6,7 @@ import type { RefIdentifier } from '../../types.js'; | ||
}; | ||
/** | ||
* Returns JS fragment to call the parse function on the union conditional | ||
* schema. | ||
*/ | ||
export declare function callParseFunction({ parseFnRefId, variableName }: FieldOptions): string; | ||
export {}; |
@@ -0,1 +1,13 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/** | ||
* Returns JS fragment to call the parse function on the union conditional | ||
* schema. | ||
*/ | ||
export function callParseFunction({ parseFnRefId, variableName }) { | ||
@@ -2,0 +14,0 @@ if (parseFnRefId) { |
@@ -0,63 +1,244 @@ | ||
/** | ||
* Represenation of a ref id | ||
*/ | ||
export type RefIdentifier = `ref://${number}`; | ||
/** | ||
* Allowed values for refs | ||
*/ | ||
export type Refs = Record<RefIdentifier, ValidationRule | TransformFn<any, any> | ParseFn | ConditionalFn<any>>; | ||
/** | ||
* Refs store to track runtime values as refs with | ||
* type safety | ||
*/ | ||
export type RefsStore = { | ||
toJSON(): Refs; | ||
/** | ||
* Track a value inside refs | ||
*/ | ||
track(value: Refs[keyof Refs]): RefIdentifier; | ||
/** | ||
* Track a validation inside refs | ||
*/ | ||
trackValidation(validation: ValidationRule): RefIdentifier; | ||
/** | ||
* Track input value parser inside refs | ||
*/ | ||
trackParser(fn: ParseFn): RefIdentifier; | ||
/** | ||
* Track output value transformer inside refs | ||
*/ | ||
trackTransformer(fn: TransformFn<any, any>): RefIdentifier; | ||
/** | ||
* Track a conditional inside refs | ||
*/ | ||
trackConditional(fn: ConditionalFn<any>): RefIdentifier; | ||
}; | ||
/** | ||
* The context shared with the entire validation pipeline. | ||
* Each field gets its own context object. | ||
*/ | ||
export type FieldContext = { | ||
/** | ||
* Field value | ||
*/ | ||
value: unknown; | ||
/** | ||
* The data property is the top-level object under validation. | ||
*/ | ||
data: any; | ||
/** | ||
* Shared metadata across the entire validation lifecycle. It can be | ||
* used to pass data between validation rules | ||
*/ | ||
meta: Record<string, any>; | ||
/** | ||
* Mutate the value of field under validation. | ||
*/ | ||
mutate(newValue: any, field: FieldContext): void; | ||
/** | ||
* Report error to the error reporter | ||
*/ | ||
report: ErrorReporterContract['report']; | ||
/** | ||
* Is this field valid. Default: true | ||
*/ | ||
isValid: boolean; | ||
/** | ||
* Is this field has value defined. | ||
*/ | ||
isDefined: boolean; | ||
/** | ||
* Wildcard path for the field. The value is a nested | ||
* pointer to the field under validation. | ||
* | ||
* In case of arrays, the `*` wildcard is used. | ||
*/ | ||
wildCardPath: string; | ||
/** | ||
* The parent property is the parent of the field. It could be an | ||
* array or an object. | ||
*/ | ||
parent: any; | ||
/** | ||
* Name of the field under validation. In case of an array, the field | ||
* name will be a number | ||
*/ | ||
name: string | number; | ||
/** | ||
* Is this field an array member | ||
*/ | ||
isArrayMember: boolean; | ||
}; | ||
/** | ||
* The shape of validation rule picked from the | ||
* refs | ||
*/ | ||
export type ValidationRule = { | ||
/** | ||
* Performs validation | ||
*/ | ||
validator(value: unknown, options: any, field: FieldContext): any; | ||
/** | ||
* Options to pass | ||
*/ | ||
options?: any; | ||
}; | ||
/** | ||
* The shape of parse function picked from the refs | ||
*/ | ||
export type ParseFn = (value: unknown, ctx: Pick<FieldContext, 'data' | 'parent' | 'meta'>) => any; | ||
/** | ||
* The shape of transform function picked from the refs | ||
*/ | ||
export type TransformFn<Input, Output> = (value: Input, field: FieldContext) => Output; | ||
/** | ||
* The shape of conditional function used for narrowing down unions. | ||
*/ | ||
export type ConditionalFn<Input> = (value: Input, field: FieldContext) => boolean; | ||
/** | ||
* Shape of a validation rule accepted by the compiler | ||
*/ | ||
export type ValidationNode = { | ||
/** | ||
* Rule implementation function id. | ||
*/ | ||
ruleFnId: RefIdentifier; | ||
/** | ||
* Is this an async rule. This flag helps creating an optimized output | ||
*/ | ||
isAsync: boolean; | ||
/** | ||
* The rules are skipped when the value of a field is "null" or "undefined". | ||
* Unless, the "implicit" flag is true | ||
*/ | ||
implicit: boolean; | ||
}; | ||
/** | ||
* Shape of field inside a schema. | ||
*/ | ||
export type FieldNode = { | ||
/** | ||
* Should the validation cycle stop after the first error. | ||
* Defaults to true | ||
*/ | ||
bail: boolean; | ||
/** | ||
* Field name refers to the name of the field under the data | ||
* object | ||
*/ | ||
fieldName: string; | ||
/** | ||
* Name of the output property. This allows validating a field with a different name, but | ||
* storing its output value with a different name. | ||
*/ | ||
propertyName: string; | ||
/** | ||
* Are we expecting this field to be undefined or null | ||
*/ | ||
isOptional: boolean; | ||
/** | ||
* Are we expecting this field to be null | ||
*/ | ||
allowNull: boolean; | ||
/** | ||
* The reference id for the parse method. Parse method is called to mutate the | ||
* initial value. The function is executed always even when value is undefined | ||
* or null. | ||
* | ||
* @see [[ParseFn]] | ||
*/ | ||
parseFnId?: RefIdentifier; | ||
/** | ||
* A set of validations to apply on the field | ||
*/ | ||
validations: ValidationNode[]; | ||
}; | ||
/** | ||
* Shape of a single field accepted by the compiler | ||
*/ | ||
export type LiteralNode = FieldNode & { | ||
type: 'literal'; | ||
/** | ||
* Transform the output value of a field. The output of this method is the | ||
* final source of truth. The function is executed at the time of writing the | ||
* value to the output. | ||
*/ | ||
transformFnId?: RefIdentifier; | ||
}; | ||
/** | ||
* Shape of the object node accepted by the compiler | ||
*/ | ||
export type ObjectNode = FieldNode & { | ||
type: 'object'; | ||
/** | ||
* Whether or not to allow unknown properties. When disabled, the | ||
* output object will have only validated properties. | ||
* | ||
* Default: false | ||
*/ | ||
allowUnknownProperties: boolean; | ||
/** | ||
* Object known properties | ||
*/ | ||
properties: CompilerNodes[]; | ||
/** | ||
* A collection of object groups to merge into the main object. | ||
* Each group is a collection of conditionals with a sub-object | ||
* inside them. | ||
*/ | ||
groups: ObjectGroupNode[]; | ||
}; | ||
/** | ||
* A compiler object group produces a single sub object based upon | ||
* the defined conditions. | ||
*/ | ||
export type ObjectGroupNode = { | ||
type: 'group'; | ||
/** | ||
* An optional function to call when all of the conditions | ||
* are false. | ||
*/ | ||
elseConditionalFnRefId?: RefIdentifier; | ||
/** | ||
* Conditions to evaluate | ||
*/ | ||
conditions: { | ||
/** | ||
* The conditional function reference id | ||
*/ | ||
conditionalFnRefId: RefIdentifier; | ||
/** | ||
* Schema to use when condition is true | ||
*/ | ||
schema: { | ||
type: 'sub_object'; | ||
/** | ||
* Object known properties | ||
*/ | ||
properties: CompilerNodes[]; | ||
/** | ||
* A collection of object groups to merge into the main object. | ||
* Each group is a collection of conditionals with a sub-object | ||
* inside them. | ||
*/ | ||
groups: ObjectGroupNode[]; | ||
@@ -67,37 +248,117 @@ }; | ||
}; | ||
/** | ||
* Shape of the tuple node accepted by the compiler | ||
*/ | ||
export type TupleNode = FieldNode & { | ||
type: 'tuple'; | ||
/** | ||
* Whether or not to allow unknown properties. When disabled, the | ||
* output array will have only validated properties. | ||
* | ||
* Default: false | ||
*/ | ||
allowUnknownProperties: boolean; | ||
/** | ||
* Tuple known properties | ||
*/ | ||
properties: CompilerNodes[]; | ||
}; | ||
/** | ||
* Shape of the record node accepted by the compiler | ||
*/ | ||
export type RecordNode = FieldNode & { | ||
type: 'record'; | ||
/** | ||
* Captures object elements | ||
*/ | ||
each: CompilerNodes; | ||
}; | ||
/** | ||
* Shape of the array node accepted by the compiler | ||
*/ | ||
export type ArrayNode = FieldNode & { | ||
type: 'array'; | ||
/** | ||
* Captures array elements | ||
*/ | ||
each: CompilerNodes; | ||
}; | ||
/** | ||
* Shape of the union node accepted by the compiler. A union is a combination | ||
* of conditionals. | ||
*/ | ||
export type UnionNode = { | ||
type: 'union'; | ||
/** | ||
* Field name refers to the name of the field under the data | ||
* object | ||
*/ | ||
fieldName: string; | ||
/** | ||
* Name of the output property. This allows validating a field with a different name, but | ||
* storing its value with a different name. | ||
*/ | ||
propertyName: string; | ||
/** | ||
* An optional function to call when all of the conditions | ||
* are false. | ||
*/ | ||
elseConditionalFnRefId?: RefIdentifier; | ||
/** | ||
* Conditions to evaluate | ||
*/ | ||
conditions: { | ||
/** | ||
* The conditional function reference id | ||
*/ | ||
conditionalFnRefId: RefIdentifier; | ||
/** | ||
* Schema to use when condition is true | ||
*/ | ||
schema: CompilerNodes; | ||
}[]; | ||
}; | ||
/** | ||
* The root of the schema | ||
*/ | ||
export type RootNode = { | ||
type: 'root'; | ||
/** | ||
* Schema at the root level | ||
*/ | ||
schema: CompilerNodes; | ||
}; | ||
/** | ||
* Known tree nodes accepted by the compiler | ||
*/ | ||
export type CompilerNodes = LiteralNode | ObjectNode | ArrayNode | UnionNode | RecordNode | TupleNode; | ||
/** | ||
* Properties of a parent node as the compiler loops through the | ||
* rules tree and constructs JS code. | ||
*/ | ||
export type CompilerParent = { | ||
type: 'array' | 'object' | 'tuple' | 'record' | 'root'; | ||
/** | ||
* Wildcard path to the field | ||
*/ | ||
wildCardPath: string; | ||
/** | ||
* Name of the variable for the parent property. The variable name | ||
* is used to lookup values from the parent | ||
*/ | ||
variableName: string; | ||
/** | ||
* Nested path to the parent field. If the parent is nested inside | ||
* an object or array. | ||
*/ | ||
fieldPathExpression: string; | ||
/** | ||
* The expression for the output value. | ||
*/ | ||
outputExpression: string; | ||
}; | ||
/** | ||
* Compiler field is used to compute the variable and property | ||
* names for the JS output. | ||
*/ | ||
export type CompilerField = { | ||
@@ -113,12 +374,45 @@ parentValueExpression: string; | ||
}; | ||
/** | ||
* The error reporter is used for reporting validation | ||
* errors. | ||
*/ | ||
export interface ErrorReporterContract { | ||
/** | ||
* A boolean to known if there are one or more | ||
* errors. | ||
*/ | ||
hasErrors: boolean; | ||
/** | ||
* Creates an instance of an exception to throw | ||
*/ | ||
createError(): Error; | ||
/** | ||
* Report error for a field | ||
*/ | ||
report(message: string, rule: string, field: FieldContext, args?: Record<string, any>): any; | ||
} | ||
/** | ||
* Messages provider is used to resolve validation error messages | ||
* during validation. | ||
*/ | ||
export interface MessagesProviderContact { | ||
/** | ||
* Returns a validation message for a given field + rule. The args | ||
* may get passed by a validation rule to share additional context. | ||
*/ | ||
getMessage(defaultMessage: string, rule: string, field: FieldContext, args?: Record<string, any>): string; | ||
} | ||
/** | ||
* Options accepted by the compiler | ||
*/ | ||
export type CompilerOptions = { | ||
/** | ||
* Convert empty string values to null for sake of | ||
* normalization | ||
*/ | ||
convertEmptyStringsToNull: boolean; | ||
/** | ||
* Provide messages to use for required, object and | ||
* array validations. | ||
*/ | ||
messages?: Partial<{ | ||
@@ -125,0 +419,0 @@ required: string; |
@@ -0,1 +1,9 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export {}; |
{ | ||
"name": "@vinejs/compiler", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Low level compiler for VineJS validator", | ||
@@ -43,10 +43,10 @@ "type": "module", | ||
"devDependencies": { | ||
"@adonisjs/eslint-config": "^1.1.6", | ||
"@adonisjs/prettier-config": "^1.1.6", | ||
"@adonisjs/tsconfig": "^1.1.6", | ||
"@adonisjs/eslint-config": "^1.1.7", | ||
"@adonisjs/prettier-config": "^1.1.7", | ||
"@adonisjs/tsconfig": "^1.1.7", | ||
"@japa/assert": "^2.0.0-1", | ||
"@japa/runner": "^3.0.0-2", | ||
"@swc/core": "^1.3.67", | ||
"@types/node": "^20.3.3", | ||
"acorn": "^8.9.0", | ||
"@japa/runner": "^3.0.0-5", | ||
"@swc/core": "^1.3.68", | ||
"@types/node": "^20.4.1", | ||
"acorn": "^8.10.0", | ||
"ajv": "^8.12.0", | ||
@@ -94,2 +94,3 @@ "benchmark": "^2.1.4", | ||
"tests/**", | ||
"bin/**", | ||
"factories/**" | ||
@@ -96,0 +97,0 @@ ] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
149184
4560