orange-orm 5.2.3-beta → 5.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/index.browser.mjs +2 -2
- package/dist/index.mjs +2 -2
- package/docs/changelog.md +2 -0
- package/package.json +1 -1
- package/src/index.d.ts +10 -10
- package/src/map.d.ts +22 -22
- package/src/table/column.js +1 -1
- package/src/table/resultToRows/newDecodeDbRow.js +1 -1
package/README.md
CHANGED
|
@@ -2058,7 +2058,7 @@ export default map;
|
|
|
2058
2058
|
|
|
2059
2059
|
<details><summary><strong>Validation</strong></summary>
|
|
2060
2060
|
<p>In the previous sections you have already seen the <strong><i>notNull()</i></strong> validator being used on some columns. This will not only generate correct typescript mapping, but also throw an error if value is set to null or undefined. However, sometimes we do not want the notNull-validator to be run on inserts. Typically, when we have an autoincremental key or server generated uuid, it does not make sense to check for null on insert. This is where <strong><i>notNullExceptInsert()</strong></i> comes to rescue. You can also create your own custom validator as shown below. The last kind of validator, is the <a href="https://ajv.js.org/json-schema.html">ajv JSON schema validator</a>. This can be used on json columns as well as any other column type.</p>
|
|
2061
|
-
<p>Custom validators receive <code>value</code> and
|
|
2061
|
+
<p>Custom validators receive <code>value</code> and a metadata object with <code>table</code>, <code>column</code>, <code>property</code>, and <code>isInsert</code>.</p>
|
|
2062
2062
|
|
|
2063
2063
|
<sub>📄 map.ts</sub>
|
|
2064
2064
|
```ts
|
|
@@ -2076,9 +2076,9 @@ let petSchema = {
|
|
|
2076
2076
|
}
|
|
2077
2077
|
};
|
|
2078
2078
|
|
|
2079
|
-
function validateName(value?: string, meta
|
|
2079
|
+
function validateName(value?: string, meta: { table: string; column: string; property: string; isInsert: boolean }) {
|
|
2080
2080
|
if (value && value.length > 10)
|
|
2081
|
-
throw new Error(`Length cannot exceed 10 characters in ${meta
|
|
2081
|
+
throw new Error(`Length cannot exceed 10 characters in ${meta.table}.${meta.column}`);
|
|
2082
2082
|
}
|
|
2083
2083
|
|
|
2084
2084
|
const map = orange.map(x => ({
|
|
@@ -2113,7 +2113,7 @@ let petSchema = {
|
|
|
2113
2113
|
|
|
2114
2114
|
function validateName(value, meta) {
|
|
2115
2115
|
if (value && value.length > 10)
|
|
2116
|
-
throw new Error(`Length cannot exceed 10 characters in ${meta
|
|
2116
|
+
throw new Error(`Length cannot exceed 10 characters in ${meta.table}.${meta.column}`);
|
|
2117
2117
|
}
|
|
2118
2118
|
|
|
2119
2119
|
const map = orange.map(x => ({
|
package/dist/index.browser.mjs
CHANGED
|
@@ -6392,7 +6392,7 @@ function requireColumn () {
|
|
|
6392
6392
|
c.notNullExceptInsert = function() {
|
|
6393
6393
|
column._notNullExceptInsert = true;
|
|
6394
6394
|
function validate(value, meta) {
|
|
6395
|
-
if (meta
|
|
6395
|
+
if (meta.isInsert)
|
|
6396
6396
|
return;
|
|
6397
6397
|
if (value === undefined || value === null)
|
|
6398
6398
|
throw new Error(`Column ${column.alias} cannot be null or undefined`);
|
|
@@ -8613,7 +8613,7 @@ function requireNewDecodeDbRow () {
|
|
|
8613
8613
|
value = purify(value);
|
|
8614
8614
|
this._dbRow[key] = value;
|
|
8615
8615
|
if (column.validate)
|
|
8616
|
-
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias });
|
|
8616
|
+
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias, isInsert: false });
|
|
8617
8617
|
updateField(this._context, table, column, this);
|
|
8618
8618
|
let emit = this._emitColumnChanged[name];
|
|
8619
8619
|
if (emit)
|
package/dist/index.mjs
CHANGED
|
@@ -6393,7 +6393,7 @@ function requireColumn () {
|
|
|
6393
6393
|
c.notNullExceptInsert = function() {
|
|
6394
6394
|
column._notNullExceptInsert = true;
|
|
6395
6395
|
function validate(value, meta) {
|
|
6396
|
-
if (meta
|
|
6396
|
+
if (meta.isInsert)
|
|
6397
6397
|
return;
|
|
6398
6398
|
if (value === undefined || value === null)
|
|
6399
6399
|
throw new Error(`Column ${column.alias} cannot be null or undefined`);
|
|
@@ -8614,7 +8614,7 @@ function requireNewDecodeDbRow () {
|
|
|
8614
8614
|
value = purify(value);
|
|
8615
8615
|
this._dbRow[key] = value;
|
|
8616
8616
|
if (column.validate)
|
|
8617
|
-
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias });
|
|
8617
|
+
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias, isInsert: false });
|
|
8618
8618
|
updateField(this._context, table, column, this);
|
|
8619
8619
|
let emit = this._emitColumnChanged[name];
|
|
8620
8620
|
if (emit)
|
package/docs/changelog.md
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
## Changelog
|
|
2
|
+
__5.2.3__
|
|
3
|
+
Pass table and column and other metadata to custom validate function [#165](https://github.com/alfateam/orange-orm/issues/165)
|
|
2
4
|
__5.2.2__
|
|
3
5
|
Bugfix: jsonOf<T>() exists in type definitions but is missing at runtime [#166](https://github.com/alfateam/orange-orm/issues/166)
|
|
4
6
|
__5.2.1__
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -11,10 +11,10 @@ declare function r(config: r.Config): unknown;
|
|
|
11
11
|
|
|
12
12
|
declare namespace r {
|
|
13
13
|
export interface ValidationMeta {
|
|
14
|
-
table
|
|
15
|
-
column
|
|
16
|
-
property
|
|
17
|
-
isInsert
|
|
14
|
+
table: string;
|
|
15
|
+
column: string;
|
|
16
|
+
property: string;
|
|
17
|
+
isInsert: boolean;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
function table(name: string): Table;
|
|
@@ -100,7 +100,7 @@ declare namespace r {
|
|
|
100
100
|
date(): DateColumnDef;
|
|
101
101
|
}
|
|
102
102
|
export interface DateColumnDef {
|
|
103
|
-
validate(validator: (value?: Date | string, meta
|
|
103
|
+
validate(validator: (value?: Date | string, meta: ValidationMeta) => void): DateColumnDef;
|
|
104
104
|
notNull(): DateColumnNotNullDef;
|
|
105
105
|
JSONSchema(schema: object, options?: Options): DateColumnDef;
|
|
106
106
|
serializable(value: boolean): DateColumnDef;
|
|
@@ -110,7 +110,7 @@ declare namespace r {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
export interface DateColumnNotNullDef {
|
|
113
|
-
validate(validator: (value: Date | string, meta
|
|
113
|
+
validate(validator: (value: Date | string, meta: ValidationMeta) => void): DateColumnNotNullDef;
|
|
114
114
|
JSONSchema(schema: object, options?: Options): DateColumnNotNullDef;
|
|
115
115
|
serializable(value: boolean): DateColumnNotNullDef;
|
|
116
116
|
as(dbName: string): DateColumnNotNullDef;
|
|
@@ -119,7 +119,7 @@ declare namespace r {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
export interface BinaryColumnDef {
|
|
122
|
-
validate(validator: (value?: Buffer | string, meta
|
|
122
|
+
validate(validator: (value?: Buffer | string, meta: ValidationMeta) => void): BinaryColumnDef;
|
|
123
123
|
notNull(): BinaryColumnNotNullDef;
|
|
124
124
|
JSONSchema(schema: object, options?: Options): BinaryColumnDef;
|
|
125
125
|
serializable(value: boolean): BinaryColumnDef;
|
|
@@ -129,7 +129,7 @@ declare namespace r {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
export interface BinaryColumnNotNullDef {
|
|
132
|
-
validate(validator: (value: Buffer | string, meta
|
|
132
|
+
validate(validator: (value: Buffer | string, meta: ValidationMeta) => void): BinaryColumnNotNullDef;
|
|
133
133
|
JSONSchema(schema: object, options?: Options): BinaryColumnNotNullDef;
|
|
134
134
|
serializable(value: boolean): BinaryColumnNotNullDef;
|
|
135
135
|
as(dbName: string): BinaryColumnNotNullDef;
|
|
@@ -138,7 +138,7 @@ declare namespace r {
|
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
export interface ColumnOf<T> {
|
|
141
|
-
validate(validator: (value?: T, meta
|
|
141
|
+
validate(validator: (value?: T, meta: ValidationMeta) => void): ColumnOf<T>;
|
|
142
142
|
notNull(): ColumnNotNullOf<T>;
|
|
143
143
|
JSONSchema(schema: object, options?: Options): ColumnOf<T>;
|
|
144
144
|
serializable(value: boolean): ColumnOf<T>;
|
|
@@ -148,7 +148,7 @@ declare namespace r {
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
export interface ColumnNotNullOf<T> {
|
|
151
|
-
validate(validator: (value: T, meta
|
|
151
|
+
validate(validator: (value: T, meta: ValidationMeta) => void): ColumnNotNullOf<T>;
|
|
152
152
|
notNull(): ColumnNotNullOf<T>;
|
|
153
153
|
JSONSchema(schema: object, options?: Options): ColumnNotNullOf<T>;
|
|
154
154
|
serializable(value: boolean): ColumnNotNullOf<T>;
|
package/src/map.d.ts
CHANGED
|
@@ -637,10 +637,10 @@ type JsonOf<T> = {
|
|
|
637
637
|
};
|
|
638
638
|
|
|
639
639
|
interface ValidationMeta {
|
|
640
|
-
table
|
|
641
|
-
column
|
|
642
|
-
property
|
|
643
|
-
isInsert
|
|
640
|
+
table: string;
|
|
641
|
+
column: string;
|
|
642
|
+
property: string;
|
|
643
|
+
isInsert: boolean;
|
|
644
644
|
}
|
|
645
645
|
|
|
646
646
|
interface ColumnType<M> {
|
|
@@ -659,90 +659,90 @@ interface ColumnType<M> {
|
|
|
659
659
|
|
|
660
660
|
type UuidValidator<M> = M extends NotNull
|
|
661
661
|
? {
|
|
662
|
-
validate(validator: (value: string, meta
|
|
662
|
+
validate(validator: (value: string, meta: ValidationMeta) => void): UuidColumnTypeDef<M>;
|
|
663
663
|
}
|
|
664
664
|
: {
|
|
665
665
|
validate(
|
|
666
|
-
validator: (value?: string | null, meta
|
|
666
|
+
validator: (value?: string | null, meta: ValidationMeta) => void
|
|
667
667
|
): UuidColumnTypeDef<M>;
|
|
668
668
|
};
|
|
669
669
|
type StringValidator<M> = M extends NotNull
|
|
670
670
|
? {
|
|
671
|
-
validate(validator: (value: string, meta
|
|
671
|
+
validate(validator: (value: string, meta: ValidationMeta) => void): StringColumnTypeDef<M>;
|
|
672
672
|
}
|
|
673
673
|
: {
|
|
674
674
|
validate(
|
|
675
|
-
validator: (value?: string | null, meta
|
|
675
|
+
validator: (value?: string | null, meta: ValidationMeta) => void
|
|
676
676
|
): StringColumnTypeDef<M>;
|
|
677
677
|
};
|
|
678
678
|
type NumericValidator<M> = M extends NotNull
|
|
679
679
|
? {
|
|
680
|
-
validate(validator: (value: number, meta
|
|
680
|
+
validate(validator: (value: number, meta: ValidationMeta) => void): NumericColumnTypeDef<M>;
|
|
681
681
|
}
|
|
682
682
|
: {
|
|
683
683
|
validate(
|
|
684
|
-
validator: (value?: number | null, meta
|
|
684
|
+
validator: (value?: number | null, meta: ValidationMeta) => void
|
|
685
685
|
): NumericColumnTypeDef<M>;
|
|
686
686
|
};
|
|
687
687
|
type BigIntValidator<M> = M extends NotNull
|
|
688
688
|
? {
|
|
689
|
-
validate(validator: (value: bigint, meta
|
|
689
|
+
validate(validator: (value: bigint, meta: ValidationMeta) => void): BigIntColumnTypeDef<M>;
|
|
690
690
|
}
|
|
691
691
|
: {
|
|
692
692
|
validate(
|
|
693
|
-
validator: (value?: bigint | null, meta
|
|
693
|
+
validator: (value?: bigint | null, meta: ValidationMeta) => void
|
|
694
694
|
): BigIntColumnTypeDef<M>;
|
|
695
695
|
};
|
|
696
696
|
type BinaryValidator<M> = M extends NotNull
|
|
697
697
|
? {
|
|
698
|
-
validate(validator: (value: string, meta
|
|
698
|
+
validate(validator: (value: string, meta: ValidationMeta) => void): BinaryColumnTypeDef<M>;
|
|
699
699
|
}
|
|
700
700
|
: {
|
|
701
701
|
validate(
|
|
702
|
-
validator: (value?: string | null, meta
|
|
702
|
+
validator: (value?: string | null, meta: ValidationMeta) => void
|
|
703
703
|
): BinaryColumnTypeDef<M>;
|
|
704
704
|
};
|
|
705
705
|
type BooleanValidator<M> = M extends NotNull
|
|
706
706
|
? {
|
|
707
|
-
validate(validator: (value: boolean, meta
|
|
707
|
+
validate(validator: (value: boolean, meta: ValidationMeta) => void): BooleanColumnTypeDef<M>;
|
|
708
708
|
}
|
|
709
709
|
: {
|
|
710
710
|
validate(
|
|
711
|
-
validator: (value?: boolean | null, meta
|
|
711
|
+
validator: (value?: boolean | null, meta: ValidationMeta) => void
|
|
712
712
|
): BooleanColumnTypeDef<M>;
|
|
713
713
|
};
|
|
714
714
|
type JSONValidator<M> = M extends NotNull
|
|
715
715
|
? {
|
|
716
716
|
validate(
|
|
717
|
-
validator: (value: ToJsonType<M>, meta
|
|
717
|
+
validator: (value: ToJsonType<M>, meta: ValidationMeta) => void
|
|
718
718
|
): JSONColumnTypeDef<M>;
|
|
719
719
|
}
|
|
720
720
|
: {
|
|
721
721
|
validate(
|
|
722
|
-
validator: (value?: ToJsonType<M> | null, meta
|
|
722
|
+
validator: (value?: ToJsonType<M> | null, meta: ValidationMeta) => void
|
|
723
723
|
): JSONColumnTypeDef<M>;
|
|
724
724
|
};
|
|
725
725
|
type DateValidator<M> = M extends NotNull
|
|
726
726
|
? {
|
|
727
727
|
validate(
|
|
728
|
-
validator: (value: string | Date, meta
|
|
728
|
+
validator: (value: string | Date, meta: ValidationMeta) => void
|
|
729
729
|
): DateColumnTypeDef<M>;
|
|
730
730
|
}
|
|
731
731
|
: {
|
|
732
732
|
validate(
|
|
733
|
-
validator: (value?: string | Date | null, meta
|
|
733
|
+
validator: (value?: string | Date | null, meta: ValidationMeta) => void
|
|
734
734
|
): DateColumnTypeDef<M>;
|
|
735
735
|
};
|
|
736
736
|
|
|
737
737
|
type DateWithTimeZoneValidator<M> = M extends NotNull
|
|
738
738
|
? {
|
|
739
739
|
validate(
|
|
740
|
-
validator: (value: string | Date, meta
|
|
740
|
+
validator: (value: string | Date, meta: ValidationMeta) => void
|
|
741
741
|
): DateWithTimeZoneColumnTypeDef<M>;
|
|
742
742
|
}
|
|
743
743
|
: {
|
|
744
744
|
validate(
|
|
745
|
-
validator: (value?: string | Date | null, meta
|
|
745
|
+
validator: (value?: string | Date | null, meta: ValidationMeta) => void
|
|
746
746
|
): DateWithTimeZoneColumnTypeDef<M>;
|
|
747
747
|
};
|
|
748
748
|
|
package/src/table/column.js
CHANGED
|
@@ -135,7 +135,7 @@ function defineColumn(column, table) {
|
|
|
135
135
|
c.notNullExceptInsert = function() {
|
|
136
136
|
column._notNullExceptInsert = true;
|
|
137
137
|
function validate(value, meta) {
|
|
138
|
-
if (meta
|
|
138
|
+
if (meta.isInsert)
|
|
139
139
|
return;
|
|
140
140
|
if (value === undefined || value === null)
|
|
141
141
|
throw new Error(`Column ${column.alias} cannot be null or undefined`);
|
|
@@ -48,7 +48,7 @@ function newDecodeDbRow(table, dbRow, filteredAliases, shouldValidate, isInsert)
|
|
|
48
48
|
value = purify(value);
|
|
49
49
|
this._dbRow[key] = value;
|
|
50
50
|
if (column.validate)
|
|
51
|
-
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias });
|
|
51
|
+
column.validate(value, { table: table._dbName, column: column._dbName, property: column.alias, isInsert: false });
|
|
52
52
|
updateField(this._context, table, column, this);
|
|
53
53
|
let emit = this._emitColumnChanged[name];
|
|
54
54
|
if (emit)
|