@tstdl/base 0.93.13 → 0.93.14
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/package.json +1 -1
- package/schema/schemas/simple.d.ts +2 -0
- package/schema/schemas/simple.js +3 -0
- package/schema/schemas/string.d.ts +1 -1
- package/schema/schemas/string.js +6 -3
- package/test2.js +6 -22
package/package.json
CHANGED
|
@@ -6,6 +6,7 @@ export type SimpleSchemaOptions<T> = SchemaOptions<T> & Coercible;
|
|
|
6
6
|
type SimpleSchemaRefinements<T> = {
|
|
7
7
|
coercers?: SimpleSchemaCoercers<T>;
|
|
8
8
|
constraints?: (SimpleSchemaConstraint<T> | null)[];
|
|
9
|
+
transformers?: SimpleSchemaTransformer<T>;
|
|
9
10
|
gotValueFormatter?: SimpleSchemaGotValueFormatter;
|
|
10
11
|
};
|
|
11
12
|
export type SimpleSchemaCoercers<T> = {
|
|
@@ -19,6 +20,7 @@ export type SimpleSchemaCoercers<T> = {
|
|
|
19
20
|
};
|
|
20
21
|
export type SimpleSchemaConstraint<T> = (value: T) => ConstraintResult;
|
|
21
22
|
export type SimpleSchemaGotValueFormatter = (value: unknown) => string;
|
|
23
|
+
export type SimpleSchemaTransformer<T> = (value: T) => T;
|
|
22
24
|
export declare abstract class SimpleSchema<T> extends Schema<T> {
|
|
23
25
|
#private;
|
|
24
26
|
constructor(expected: OneOrMany<string | AbstractConstructor>, guardFn: (value: any) => value is T, options?: SimpleSchemaOptions<T>, refinements?: SimpleSchemaRefinements<T>);
|
package/schema/schemas/simple.js
CHANGED
|
@@ -8,6 +8,7 @@ export class SimpleSchema extends Schema {
|
|
|
8
8
|
#options;
|
|
9
9
|
#coercers;
|
|
10
10
|
#constraints;
|
|
11
|
+
#transformers;
|
|
11
12
|
#gotValueFormatter;
|
|
12
13
|
constructor(expected, guardFn, options = {}, refinements = {}) {
|
|
13
14
|
super(options);
|
|
@@ -16,6 +17,7 @@ export class SimpleSchema extends Schema {
|
|
|
16
17
|
this.#options = options;
|
|
17
18
|
this.#coercers = refinements.coercers ?? {};
|
|
18
19
|
this.#constraints = refinements.constraints?.filter(isNotNull) ?? [];
|
|
20
|
+
this.#transformers = refinements.transformers ?? ((value) => value);
|
|
19
21
|
this.#gotValueFormatter = refinements.gotValueFormatter ?? typeOf;
|
|
20
22
|
}
|
|
21
23
|
_test(value, path, options) {
|
|
@@ -52,6 +54,7 @@ export class SimpleSchema extends Schema {
|
|
|
52
54
|
return { valid: false, error: new SchemaError(`Constraint validation failed: ${constraintResult.error}`, { path, fast: options.fastErrors }) };
|
|
53
55
|
}
|
|
54
56
|
}
|
|
57
|
+
result.value = this.#transformers(result.value);
|
|
55
58
|
return result;
|
|
56
59
|
}
|
|
57
60
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type SchemaDecoratorOptions, type SchemaPropertyDecorator } from '../decorators/index.js';
|
|
2
2
|
import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
|
|
3
3
|
export type StringSchemaOptions = SimpleSchemaOptions<string> & {
|
|
4
4
|
pattern?: RegExp | string;
|
package/schema/schemas/string.js
CHANGED
|
@@ -10,11 +10,14 @@ export class StringSchema extends SimpleSchema {
|
|
|
10
10
|
coercers: {
|
|
11
11
|
number: (value) => ({ success: true, value: globalThis.String(value), valid: true }),
|
|
12
12
|
boolean: (value) => ({ success: true, value: globalThis.String(value), valid: true }),
|
|
13
|
-
bigint: (value) => ({ success: true, value: globalThis.String(value), valid: true })
|
|
13
|
+
bigint: (value) => ({ success: true, value: globalThis.String(value), valid: true }),
|
|
14
14
|
},
|
|
15
15
|
constraints: [
|
|
16
|
-
isDefined(options?.pattern)
|
|
17
|
-
|
|
16
|
+
isDefined(options?.pattern)
|
|
17
|
+
? (value) => this.pattern.test(value) ? ({ success: true }) : ({ success: false, error: 'Value did not match pattern.' })
|
|
18
|
+
: null,
|
|
19
|
+
],
|
|
20
|
+
transformers: (options?.lowercase ?? false) ? (value) => value.toLowerCase() : undefined,
|
|
18
21
|
});
|
|
19
22
|
this.pattern = isString(options?.pattern)
|
|
20
23
|
? RegExp(options.pattern, 'u')
|
package/test2.js
CHANGED
|
@@ -1,30 +1,14 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
1
|
import { inject, Singleton } from './injector/index.js';
|
|
8
2
|
import { Application } from './application/application.js';
|
|
9
3
|
import { provideModule } from './application/providers.js';
|
|
10
4
|
import { PrettyPrintLogFormatter, provideConsoleLogTransport } from './logger/index.js';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
Singleton()
|
|
16
|
-
], Foo);
|
|
17
|
-
let Bar = class Bar {
|
|
18
|
-
};
|
|
19
|
-
Bar = __decorate([
|
|
20
|
-
Singleton({
|
|
21
|
-
argumentIdentityProvider() {
|
|
22
|
-
throw new Error('haha');
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
|
-
], Bar);
|
|
5
|
+
import { object, string } from './schema/index.js';
|
|
6
|
+
const schema = object({
|
|
7
|
+
value: string({ lowercase: true })
|
|
8
|
+
});
|
|
26
9
|
function main() {
|
|
27
|
-
const
|
|
10
|
+
const value = schema.parse({ value: 'HELLO WORLD' });
|
|
11
|
+
console.log(value);
|
|
28
12
|
}
|
|
29
13
|
Application.run('Test', [
|
|
30
14
|
provideModule(main),
|