@muziehdesign/forms 0.0.1-150

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.
@@ -0,0 +1,304 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, Component, Input, NgModule } from '@angular/core';
3
+ import * as Yup from 'yup';
4
+ import { object } from 'yup';
5
+ import 'reflect-metadata';
6
+ import * as i1 from '@angular/common';
7
+ import { CommonModule } from '@angular/common';
8
+ import { __awaiter } from 'tslib';
9
+ import { BehaviorSubject, switchMap, from } from 'rxjs';
10
+
11
+ class ModelValidator {
12
+ constructor(modelSchema) {
13
+ this.schema = modelSchema;
14
+ }
15
+ validate(model) {
16
+ return this.schema
17
+ .validate(model, { abortEarly: false })
18
+ .then(() => {
19
+ return [];
20
+ })
21
+ .catch((e) => {
22
+ return e.inner.map((error) => ({ path: error.path, type: error.type, message: error.message }));
23
+ });
24
+ }
25
+ }
26
+
27
+ const SCHEMA_METADATA_NAMESPACE = 'custom:muziehdesign:annotations';
28
+
29
+ const METADATA_KEY = 'custom:muziehdesign:annotations';
30
+ var ConstraintType;
31
+ (function (ConstraintType) {
32
+ ConstraintType[ConstraintType["string"] = 0] = "string";
33
+ ConstraintType[ConstraintType["boolean"] = 1] = "boolean";
34
+ ConstraintType[ConstraintType["date"] = 2] = "date";
35
+ })(ConstraintType || (ConstraintType = {}));
36
+ const registerMetadata = (target, propertyKey, constraint) => {
37
+ const metadata = Reflect.getMetadata(METADATA_KEY, target) || new Map();
38
+ metadata.set(propertyKey, constraint);
39
+ Reflect.defineMetadata(METADATA_KEY, metadata, target);
40
+ };
41
+ function StringType(...annotations) {
42
+ return function (target, propertyKey) {
43
+ const o = Object.assign({}, ...annotations);
44
+ o.constraintType = ConstraintType.string;
45
+ registerMetadata(target, propertyKey, o);
46
+ };
47
+ }
48
+ function BooleanType(...annotations) {
49
+ return function (target, propertyKey) {
50
+ const o = Object.assign({}, ...annotations);
51
+ o.constraintType = ConstraintType.boolean;
52
+ registerMetadata(target, propertyKey, o);
53
+ };
54
+ }
55
+ function DateType(...annotations) {
56
+ return function (target, propertyKey) {
57
+ const o = Object.assign({}, ...annotations);
58
+ o.constraintType = ConstraintType.date;
59
+ registerMetadata(target, propertyKey, o);
60
+ };
61
+ }
62
+ function required(message) {
63
+ return { required: { required: true, message: message } };
64
+ }
65
+ function pattern(regex, message) {
66
+ return { pattern: { pattern: regex, message: message } };
67
+ }
68
+ function length(length, message) {
69
+ return { length: { length: length, message: message } };
70
+ }
71
+ function maxLength(maxLength, message) {
72
+ return { maxLength: { maxLength: maxLength, message: message } };
73
+ }
74
+ function minLength(minLength, message) {
75
+ return { minLength: { minLength: minLength, message: message } };
76
+ }
77
+ function ofValues(values, message) {
78
+ return { ofValues: { values: values, message: message } };
79
+ }
80
+ function equals(value, message) {
81
+ return { equals: { equals: value, message: message } };
82
+ }
83
+ function min(value, message) {
84
+ return { min: { min: value, message: message } };
85
+ }
86
+ function max(value, message) {
87
+ return { max: { max: value, message: message } };
88
+ }
89
+ function test(name, test, message) {
90
+ return { test: { name: name, test: test, message: message } };
91
+ }
92
+
93
+ /*
94
+ Schema rules need to be built in the order they need to be evaluated in.
95
+ For example,
96
+ ```
97
+ schema.required().max(...).matches(...);
98
+ ```
99
+ evaluates the 3 rules in this order
100
+ - required
101
+ - max
102
+ - matches
103
+ */
104
+ class ModelSchemaFactory {
105
+ constructor() { }
106
+ build(model) {
107
+ const metadata = Reflect.getMetadata(SCHEMA_METADATA_NAMESPACE, model);
108
+ let shape = {};
109
+ metadata.forEach((value, key) => {
110
+ if (value.constraintType == ConstraintType.string) {
111
+ shape[key] = this.buildStringSchema(value);
112
+ }
113
+ else if (value.constraintType == ConstraintType.boolean) {
114
+ shape[key] = this.buildBooleanSchema(value);
115
+ }
116
+ else if (value.constraintType == ConstraintType.date) {
117
+ shape[key] = this.buildDateSchema(value);
118
+ }
119
+ });
120
+ const schema = object(shape);
121
+ return new ModelValidator(schema);
122
+ }
123
+ buildStringSchema(options) {
124
+ let schema = Yup.string();
125
+ if (options.required) {
126
+ schema = schema.required(options.required.message);
127
+ }
128
+ if (options.length) {
129
+ schema = schema.length(options.length.length, options.length.message);
130
+ }
131
+ if (options.maxLength) {
132
+ schema = schema.max(options.maxLength.maxLength, options.maxLength.message);
133
+ }
134
+ if (options.minLength) {
135
+ schema = schema.min(options.minLength.minLength, options.minLength.message);
136
+ }
137
+ if (options.pattern) {
138
+ schema = schema.matches(options.pattern.pattern, options.pattern.message);
139
+ }
140
+ return schema;
141
+ }
142
+ buildBooleanSchema(options) {
143
+ let schema = Yup.boolean();
144
+ if (options.required) {
145
+ schema = schema.required(options.required.message);
146
+ }
147
+ if (options.equals) {
148
+ if (options.equals.equals) {
149
+ schema = schema.isTrue(options.equals.message);
150
+ }
151
+ else {
152
+ schema = schema.isFalse(options.equals.message);
153
+ }
154
+ }
155
+ return schema;
156
+ }
157
+ buildDateSchema(options) {
158
+ let schema = Yup.date().typeError('Please enter a valid date');
159
+ if (options.required) {
160
+ schema = schema.required(options.required.message);
161
+ }
162
+ if (options.min) {
163
+ schema = schema.min(options.min.min, options.min.message);
164
+ }
165
+ if (options.max) {
166
+ schema = schema.max(options.max.max, options.max.message);
167
+ }
168
+ if (options.test) {
169
+ schema = schema.test({
170
+ name: options.test.name,
171
+ message: options.test.message,
172
+ test: (d, context) => {
173
+ return options.test.test(d);
174
+ }
175
+ });
176
+ }
177
+ return schema;
178
+ }
179
+ }
180
+ ModelSchemaFactory.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
181
+ ModelSchemaFactory.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, providedIn: 'root' });
182
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, decorators: [{
183
+ type: Injectable,
184
+ args: [{
185
+ providedIn: 'root',
186
+ }]
187
+ }], ctorParameters: function () { return []; } });
188
+
189
+ class FieldErrorsComponent {
190
+ get errorMessage() {
191
+ var _a, _b;
192
+ const errorKeys = Object.keys(((_a = this.field) === null || _a === void 0 ? void 0 : _a.errors) || {});
193
+ return errorKeys.length > 0 ? (_b = this.field) === null || _b === void 0 ? void 0 : _b.errors[errorKeys[0]] : '';
194
+ }
195
+ }
196
+ FieldErrorsComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FieldErrorsComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
197
+ FieldErrorsComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.0.5", type: FieldErrorsComponent, selector: "mz-field-errors", inputs: { field: "field" }, ngImport: i0, template: "<div class=\"field-error\" *ngIf=\"errorMessage\">{{ errorMessage }}</div>\n", styles: [""], dependencies: [{ kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }] });
198
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FieldErrorsComponent, decorators: [{
199
+ type: Component,
200
+ args: [{ selector: 'mz-field-errors', template: "<div class=\"field-error\" *ngIf=\"errorMessage\">{{ errorMessage }}</div>\n" }]
201
+ }], propDecorators: { field: [{
202
+ type: Input
203
+ }] } });
204
+
205
+ class FormsModule {
206
+ }
207
+ FormsModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
208
+ FormsModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, declarations: [FieldErrorsComponent], imports: [CommonModule // TODO: can remove once done with temp error displaying
209
+ ], exports: [FieldErrorsComponent] });
210
+ FormsModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, imports: [CommonModule // TODO: can remove once done with temp error displaying
211
+ ] });
212
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, decorators: [{
213
+ type: NgModule,
214
+ args: [{
215
+ providers: [],
216
+ declarations: [
217
+ FieldErrorsComponent
218
+ ],
219
+ exports: [
220
+ FieldErrorsComponent
221
+ ],
222
+ imports: [
223
+ CommonModule // TODO: can remove once done with temp error displaying
224
+ ]
225
+ }]
226
+ }] });
227
+
228
+ class NgFormModelStateFactory {
229
+ constructor(factory) {
230
+ this.factory = factory;
231
+ }
232
+ create(form, model, options) {
233
+ const modelState = new NgFormModelState(form, this.factory.build(model), model, options);
234
+ return modelState;
235
+ }
236
+ }
237
+ NgFormModelStateFactory.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, deps: [{ token: ModelSchemaFactory }], target: i0.ɵɵFactoryTarget.Injectable });
238
+ NgFormModelStateFactory.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, providedIn: 'root' });
239
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, decorators: [{
240
+ type: Injectable,
241
+ args: [{
242
+ providedIn: 'root',
243
+ }]
244
+ }], ctorParameters: function () { return [{ type: ModelSchemaFactory }]; } });
245
+ class NgFormModelState {
246
+ constructor(form, modelValidator, model, options) {
247
+ this.form = form;
248
+ this.modelValidator = modelValidator;
249
+ this.model = model;
250
+ this.errors = new BehaviorSubject([]);
251
+ this.errors$ = this.errors.asObservable();
252
+ this.options = options;
253
+ this.form.form.valueChanges
254
+ .pipe(switchMap((x) => __awaiter(this, void 0, void 0, function* () {
255
+ var _a;
256
+ this.model = x;
257
+ return from(this.runValidations((_a = this.options) === null || _a === void 0 ? void 0 : _a.onValidate));
258
+ })))
259
+ .subscribe();
260
+ this.errors$.subscribe((list) => {
261
+ const grouped = list.reduce((grouped, v) => grouped.set(v.path, [...(grouped.get(v.path) || []), v]), new Map());
262
+ grouped.forEach((value, key) => {
263
+ var _a;
264
+ let validationErrors = {};
265
+ value.forEach((v) => (validationErrors[v.type] = v.message));
266
+ (_a = this.form.controls[key]) === null || _a === void 0 ? void 0 : _a.setErrors(validationErrors);
267
+ });
268
+ });
269
+ }
270
+ isValid() {
271
+ return this.errors.value.length == 0;
272
+ }
273
+ setErrors(errors) {
274
+ this.errors.next(errors);
275
+ }
276
+ validate() {
277
+ var _a;
278
+ return __awaiter(this, void 0, void 0, function* () {
279
+ return yield this.runValidations((_a = this.options) === null || _a === void 0 ? void 0 : _a.onValidate);
280
+ });
281
+ }
282
+ runValidations(callback) {
283
+ return __awaiter(this, void 0, void 0, function* () {
284
+ this.removeCurrentErrors();
285
+ const list = yield this.modelValidator.validate(this.model);
286
+ const final = (callback === null || callback === void 0 ? void 0 : callback(list)) || list;
287
+ this.errors.next(final);
288
+ });
289
+ }
290
+ removeCurrentErrors() {
291
+ Object.keys(this.form.controls).forEach(key => this.form.controls[key].setErrors(null));
292
+ }
293
+ }
294
+
295
+ /*
296
+ * Public API Surface of forms
297
+ */
298
+
299
+ /**
300
+ * Generated bundle index. Do not edit.
301
+ */
302
+
303
+ export { BooleanType, ConstraintType, DateType, FieldErrorsComponent, FormsModule, ModelSchemaFactory, ModelValidator, NgFormModelState, NgFormModelStateFactory, StringType, equals, length, max, maxLength, min, minLength, ofValues, pattern, required, test };
304
+ //# sourceMappingURL=muziehdesign-forms.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"muziehdesign-forms.mjs","sources":["../../../../projects/muziehdesign/forms/src/lib/model-validator.ts","../../../../projects/muziehdesign/forms/src/lib/constants.ts","../../../../projects/muziehdesign/forms/src/lib/type-annotations.ts","../../../../projects/muziehdesign/forms/src/lib/model-schema.factory.ts","../../../../projects/muziehdesign/forms/src/lib/field-errors/field-errors.component.ts","../../../../projects/muziehdesign/forms/src/lib/field-errors/field-errors.component.html","../../../../projects/muziehdesign/forms/src/lib/forms.module.ts","../../../../projects/muziehdesign/forms/src/lib/ng-form-model-state.service.ts","../../../../projects/muziehdesign/forms/src/public-api.ts","../../../../projects/muziehdesign/forms/src/muziehdesign-forms.ts"],"sourcesContent":["import { SchemaOf, ValidationError } from 'yup';\nimport { FieldError } from './field-error';\n\nexport class ModelValidator<T> {\n private schema: SchemaOf<T>;\n constructor(modelSchema: SchemaOf<T>) {\n this.schema = modelSchema;\n }\n\n validate<T>(model: T): Promise<FieldError[]> {\n return this.schema\n .validate(model, { abortEarly: false })\n .then(() => {\n return [];\n })\n .catch((e: ValidationError) => {\n return e.inner.map((error) => <FieldError>{ path: error.path, type: error.type, message: error.message });\n });\n }\n}\n","export const SCHEMA_METADATA_NAMESPACE = 'custom:muziehdesign:annotations';\n","import 'reflect-metadata';\n\nconst METADATA_KEY = 'custom:muziehdesign:annotations';\n\nexport enum ConstraintType {\n string,\n boolean,\n date,\n}\n\nexport interface ConstraintAnnotations {\n constraintType: ConstraintType;\n}\n\nexport interface StringTypeAnnotations extends ConstraintAnnotations {\n required?: RequiredAnnotation;\n length?: LengthAnnotation;\n pattern?: PatternAnnotation;\n maxLength?: MaxLengthAnnotation;\n minLength?: MinLengthAnnotation;\n}\n\nexport interface BooleanTypeAnnotations extends ConstraintAnnotations {\n required?: RequiredAnnotation;\n equals?: EqualsAnnotation<boolean>;\n}\n\nexport interface DateTypeAnnotations extends ConstraintAnnotations {\n required?: RequiredAnnotation;\n min?: MinimumAnnotation<Date>;\n max?: MaximumAnnotation<Date>;\n test?: TestAnnotation<Date>;\n}\n\nexport interface ValidationAnnotation {\n message?: string;\n}\n\nexport interface RequiredAnnotation extends ValidationAnnotation {\n required: boolean;\n}\n\nexport interface LengthAnnotation extends ValidationAnnotation {\n length: number;\n}\n\nexport interface PatternAnnotation extends ValidationAnnotation {\n pattern: RegExp;\n}\n\nexport interface OfValuesAnnotation extends ValidationAnnotation {\n values: [];\n}\n\nexport interface EqualsAnnotation<T> extends ValidationAnnotation {\n equals: T;\n}\n\nexport interface MinimumAnnotation<T> extends ValidationAnnotation {\n min: T;\n}\n\nexport interface MaximumAnnotation<T> extends ValidationAnnotation {\n max: T;\n}\n\nexport interface TestAnnotation<T> extends ValidationAnnotation {\n test: (d:T) => boolean;\n name: string;\n}\n\nexport interface MaxLengthAnnotation extends ValidationAnnotation {\n maxLength: number;\n}\n\nexport interface MinLengthAnnotation extends ValidationAnnotation {\n minLength: number;\n}\n\nconst registerMetadata = (target: Object, propertyKey: string, constraint: ConstraintAnnotations) => {\n const metadata: Map<string, any> = Reflect.getMetadata(METADATA_KEY, target) || new Map<string, any>();\n metadata.set(propertyKey, constraint);\n Reflect.defineMetadata(METADATA_KEY, metadata, target);\n};\n\nexport function StringType(...annotations: { [key: string]: ValidationAnnotation }[]) {\n return function (target: Object, propertyKey: string) {\n const o = Object.assign({}, ...annotations) as StringTypeAnnotations;\n o.constraintType = ConstraintType.string;\n registerMetadata(target, propertyKey, o);\n };\n}\n\nexport function BooleanType(...annotations: { [key: string]: ValidationAnnotation }[]) {\n return function (target: Object, propertyKey: string) {\n const o = Object.assign({}, ...annotations) as BooleanTypeAnnotations;\n o.constraintType = ConstraintType.boolean;\n registerMetadata(target, propertyKey, o);\n };\n}\n\nexport function DateType(...annotations: { [key: string]: ValidationAnnotation }[]) {\n return function (target: Object, propertyKey: string) {\n const o = Object.assign({}, ...annotations) as DateTypeAnnotations;\n o.constraintType = ConstraintType.date;\n registerMetadata(target, propertyKey, o);\n };\n}\n\nexport function required(message?: string): { [key: string]: RequiredAnnotation } {\n return { required: { required: true, message: message } };\n}\n\nexport function pattern(regex: RegExp, message?: string): { [key: string]: PatternAnnotation } {\n return { pattern: { pattern: regex, message: message } };\n}\n\nexport function length(length: number, message?: string): { [key: string]: LengthAnnotation } {\n return { length: { length: length, message: message } };\n}\n\nexport function maxLength(maxLength: number, message?: string): { [key: string]: MaxLengthAnnotation } {\n return { maxLength: { maxLength: maxLength, message: message } };\n}\n\nexport function minLength(minLength: number, message?: string): { [key: string]: MinLengthAnnotation } {\n return { minLength: { minLength: minLength, message: message } };\n}\n\nexport function ofValues(values: [], message?: string): { [key: string]: OfValuesAnnotation } {\n return { ofValues: { values: values, message: message } };\n}\n\nexport function equals<T>(value: T, message?: string): { [key: string]: EqualsAnnotation<T> } {\n return { equals: { equals: value, message: message } };\n}\n\nexport function min<T>(value: T, message?: string): { [key: string]: MinimumAnnotation<T> } {\n return { min: { min: value, message: message } };\n}\n\nexport function max<T>(value: T, message?: string): { [key: string]: MaximumAnnotation<T> } {\n return { max: { max: value, message: message } };\n}\n\nexport function test<T>(name: string, test: (d:T) => boolean, message?: string): { [key: string]: TestAnnotation<T> } {\n return { test: { name: name, test: test, message: message } };\n}\n","import { Injectable } from '@angular/core';\nimport { object, SchemaOf } from 'yup';\nimport { ModelValidator } from './model-validator';\nimport { SCHEMA_METADATA_NAMESPACE } from './constants';\nimport { ObjectShape } from 'yup/lib/object';\nimport { BooleanTypeAnnotations, ConstraintAnnotations, ConstraintType, DateTypeAnnotations, StringTypeAnnotations } from './type-annotations';\nimport * as Yup from 'yup';\n\n/*\nSchema rules need to be built in the order they need to be evaluated in.\nFor example,\n ```\n schema.required().max(...).matches(...);\n ```\nevaluates the 3 rules in this order\n - required\n - max\n - matches\n*/\n\n@Injectable({\n providedIn: 'root',\n})\nexport class ModelSchemaFactory {\n constructor() {}\n\n build<T>(model: T): ModelValidator<T> {\n const metadata: Map<string, ConstraintAnnotations> = Reflect.getMetadata(SCHEMA_METADATA_NAMESPACE, model);\n let shape: ObjectShape = {};\n metadata.forEach((value, key) => {\n if (value.constraintType == ConstraintType.string) {\n shape[key] = this.buildStringSchema(value as StringTypeAnnotations);\n } else if (value.constraintType == ConstraintType.boolean) {\n shape[key] = this.buildBooleanSchema(value as BooleanTypeAnnotations);\n } else if (value.constraintType == ConstraintType.date) {\n shape[key] = this.buildDateSchema(value as DateTypeAnnotations);\n }\n });\n const schema = object(shape) as SchemaOf<T>;\n return new ModelValidator(schema);\n }\n\n private buildStringSchema(options: StringTypeAnnotations) {\n let schema = Yup.string();\n if (options.required) {\n schema = schema.required(options.required.message);\n }\n if (options.length) {\n schema = schema.length(options.length.length, options.length.message);\n }\n\n if (options.maxLength) {\n schema = schema.max(options.maxLength.maxLength, options.maxLength.message);\n }\n\n if (options.minLength) {\n schema = schema.min(options.minLength.minLength, options.minLength.message);\n }\n\n if (options.pattern) {\n schema = schema.matches(options.pattern.pattern, options.pattern.message);\n }\n\n return schema;\n }\n\n private buildBooleanSchema(options: BooleanTypeAnnotations) {\n let schema = Yup.boolean();\n if (options.required) {\n schema = schema.required(options.required.message);\n }\n if (options.equals) {\n if (options.equals.equals) {\n schema = schema.isTrue(options.equals.message);\n } else {\n schema = schema.isFalse(options.equals.message);\n }\n }\n\n return schema;\n }\n\n private buildDateSchema(options: DateTypeAnnotations) {\n let schema = Yup.date().typeError('Please enter a valid date');\n if (options.required) {\n schema = schema.required(options.required.message);\n }\n if (options.min) {\n schema = schema.min(options.min.min, options.min.message);\n }\n if (options.max) {\n schema = schema.max(options.max.max, options.max.message);\n }\n if (options.test) {\n schema = schema.test({\n name: options.test.name,\n message: options.test.message,\n test: (d?: Date, context?: any) => {\n return options.test!.test(d!);\n }});\n }\n\n return schema;\n }\n}\n","import { Component, Input, OnInit } from '@angular/core';\nimport { NgControl, ValidationErrors } from '@angular/forms';\n\n@Component({\n selector: 'mz-field-errors',\n templateUrl: './field-errors.component.html',\n styleUrls: ['./field-errors.component.css'],\n})\nexport class FieldErrorsComponent {\n @Input() field?: NgControl;\n\n get errorMessage(): string {\n const errorKeys = Object.keys(this.field?.errors || {});\n return errorKeys.length > 0 ? (this.field?.errors as ValidationErrors)[errorKeys[0]] : '';\n }\n}\n","<div class=\"field-error\" *ngIf=\"errorMessage\">{{ errorMessage }}</div>\n","import { CommonModule } from '@angular/common';\nimport { NgModule } from '@angular/core';\nimport { FieldErrorsComponent } from './field-errors/field-errors.component';\n\n\n\n@NgModule({\n providers: [],\n declarations: [\n FieldErrorsComponent\n ],\n exports: [\n FieldErrorsComponent\n ],\n imports: [\n CommonModule // TODO: can remove once done with temp error displaying\n ]\n})\nexport class FormsModule { }\n","import { Injectable, NO_ERRORS_SCHEMA } from '@angular/core';\nimport { NgForm, ValidationErrors } from '@angular/forms';\nimport { BehaviorSubject, filter, from, switchMap } from 'rxjs';\nimport { FieldError } from './field-error';\nimport { ModelSchemaFactory } from './model-schema.factory';\nimport { ModelValidator } from './model-validator';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class NgFormModelStateFactory {\n constructor(private factory: ModelSchemaFactory) {}\n\n create<T>(form: NgForm, model: T, options?: ModelStateOptions) {\n const modelState = new NgFormModelState<T>(form, this.factory.build(model), model, options);\n return modelState;\n }\n}\n\nexport interface ModelStateOptions {\n onValidate: (errors: FieldError[]) => FieldError[]\n}\n\nexport class NgFormModelState<T> {\n private errors: BehaviorSubject<FieldError[]> = new BehaviorSubject<FieldError[]>([]);\n public errors$ = this.errors.asObservable();\n private options?: ModelStateOptions;\n\n constructor(private form: NgForm, private modelValidator: ModelValidator<T>, private model: T, options?: ModelStateOptions) {\n this.options = options;\n \n this.form.form.valueChanges\n .pipe(\n switchMap(async (x) => {\n this.model = x;\n return from(this.runValidations(this.options?.onValidate))\n })\n )\n .subscribe();\n\n this.errors$.subscribe((list) => {\n const grouped = list.reduce((grouped, v) => grouped.set(v.path, [...(grouped.get(v.path) || []), v]), new Map<string, FieldError[]>());\n\n grouped.forEach((value, key) => {\n let validationErrors = <ValidationErrors>{};\n value.forEach((v) => (validationErrors[v.type] = v.message));\n this.form.controls[key]?.setErrors(validationErrors);\n });\n });\n }\n\n isValid(): boolean {\n return this.errors.value.length == 0;\n }\n\n setErrors(errors: FieldError[]) {\n this.errors.next(errors);\n }\n\n async validate(): Promise<void> {\n return await this.runValidations(this.options?.onValidate);\n }\n\n private async runValidations(callback?: (list:FieldError[])=>FieldError[]): Promise<void> {\n this.removeCurrentErrors();\n const list = await this.modelValidator.validate(this.model);\n const final = callback?.(list) || list;\n this.errors.next(final);\n }\n\n private removeCurrentErrors() {\n Object.keys(this.form.controls).forEach(key => this.form.controls[key].setErrors(null));\n }\n}\n","/*\n * Public API Surface of forms\n */\nexport * from './lib/field-error';\nexport * from './lib/model-schema.factory';\nexport * from './lib/model-validator';\nexport * from './lib/type-annotations';\nexport * from './lib/forms.module';\nexport * from './lib/ng-form-model-state.service';\nexport * from './lib/field-errors/field-errors.component';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":["i1.ModelSchemaFactory"],"mappings":";;;;;;;;;;MAGa,cAAc,CAAA;AAEzB,IAAA,WAAA,CAAY,WAAwB,EAAA;AAClC,QAAA,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC;KAC3B;AAED,IAAA,QAAQ,CAAI,KAAQ,EAAA;QAClB,OAAO,IAAI,CAAC,MAAM;aACf,QAAQ,CAAC,KAAK,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;aACtC,IAAI,CAAC,MAAK;AACT,YAAA,OAAO,EAAE,CAAC;AACZ,SAAC,CAAC;AACD,aAAA,KAAK,CAAC,CAAC,CAAkB,KAAI;AAC5B,YAAA,OAAO,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,MAAiB,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAA,CAAC,CAAC;AAC5G,SAAC,CAAC,CAAC;KACN;AACF;;ACnBM,MAAM,yBAAyB,GAAG,iCAAiC;;ACE1E,MAAM,YAAY,GAAG,iCAAiC,CAAC;AAE3C,IAAA,eAIX;AAJD,CAAA,UAAY,cAAc,E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AC,KAAK,CAAC,CAAC;SACzB,CAAA,CAAA;AAAA,KAAA;IAEO,mBAAmB,GAAA;AACzB,QAAA,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;KACzF;AACF;;ACzED;;AAEG;;ACFH;;AAEG;;;;"}
@@ -0,0 +1,295 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, Component, Input, NgModule } from '@angular/core';
3
+ import * as Yup from 'yup';
4
+ import { object } from 'yup';
5
+ import 'reflect-metadata';
6
+ import * as i1 from '@angular/common';
7
+ import { CommonModule } from '@angular/common';
8
+ import { BehaviorSubject, switchMap, from } from 'rxjs';
9
+
10
+ class ModelValidator {
11
+ constructor(modelSchema) {
12
+ this.schema = modelSchema;
13
+ }
14
+ validate(model) {
15
+ return this.schema
16
+ .validate(model, { abortEarly: false })
17
+ .then(() => {
18
+ return [];
19
+ })
20
+ .catch((e) => {
21
+ return e.inner.map((error) => ({ path: error.path, type: error.type, message: error.message }));
22
+ });
23
+ }
24
+ }
25
+
26
+ const SCHEMA_METADATA_NAMESPACE = 'custom:muziehdesign:annotations';
27
+
28
+ const METADATA_KEY = 'custom:muziehdesign:annotations';
29
+ var ConstraintType;
30
+ (function (ConstraintType) {
31
+ ConstraintType[ConstraintType["string"] = 0] = "string";
32
+ ConstraintType[ConstraintType["boolean"] = 1] = "boolean";
33
+ ConstraintType[ConstraintType["date"] = 2] = "date";
34
+ })(ConstraintType || (ConstraintType = {}));
35
+ const registerMetadata = (target, propertyKey, constraint) => {
36
+ const metadata = Reflect.getMetadata(METADATA_KEY, target) || new Map();
37
+ metadata.set(propertyKey, constraint);
38
+ Reflect.defineMetadata(METADATA_KEY, metadata, target);
39
+ };
40
+ function StringType(...annotations) {
41
+ return function (target, propertyKey) {
42
+ const o = Object.assign({}, ...annotations);
43
+ o.constraintType = ConstraintType.string;
44
+ registerMetadata(target, propertyKey, o);
45
+ };
46
+ }
47
+ function BooleanType(...annotations) {
48
+ return function (target, propertyKey) {
49
+ const o = Object.assign({}, ...annotations);
50
+ o.constraintType = ConstraintType.boolean;
51
+ registerMetadata(target, propertyKey, o);
52
+ };
53
+ }
54
+ function DateType(...annotations) {
55
+ return function (target, propertyKey) {
56
+ const o = Object.assign({}, ...annotations);
57
+ o.constraintType = ConstraintType.date;
58
+ registerMetadata(target, propertyKey, o);
59
+ };
60
+ }
61
+ function required(message) {
62
+ return { required: { required: true, message: message } };
63
+ }
64
+ function pattern(regex, message) {
65
+ return { pattern: { pattern: regex, message: message } };
66
+ }
67
+ function length(length, message) {
68
+ return { length: { length: length, message: message } };
69
+ }
70
+ function maxLength(maxLength, message) {
71
+ return { maxLength: { maxLength: maxLength, message: message } };
72
+ }
73
+ function minLength(minLength, message) {
74
+ return { minLength: { minLength: minLength, message: message } };
75
+ }
76
+ function ofValues(values, message) {
77
+ return { ofValues: { values: values, message: message } };
78
+ }
79
+ function equals(value, message) {
80
+ return { equals: { equals: value, message: message } };
81
+ }
82
+ function min(value, message) {
83
+ return { min: { min: value, message: message } };
84
+ }
85
+ function max(value, message) {
86
+ return { max: { max: value, message: message } };
87
+ }
88
+ function test(name, test, message) {
89
+ return { test: { name: name, test: test, message: message } };
90
+ }
91
+
92
+ /*
93
+ Schema rules need to be built in the order they need to be evaluated in.
94
+ For example,
95
+ ```
96
+ schema.required().max(...).matches(...);
97
+ ```
98
+ evaluates the 3 rules in this order
99
+ - required
100
+ - max
101
+ - matches
102
+ */
103
+ class ModelSchemaFactory {
104
+ constructor() { }
105
+ build(model) {
106
+ const metadata = Reflect.getMetadata(SCHEMA_METADATA_NAMESPACE, model);
107
+ let shape = {};
108
+ metadata.forEach((value, key) => {
109
+ if (value.constraintType == ConstraintType.string) {
110
+ shape[key] = this.buildStringSchema(value);
111
+ }
112
+ else if (value.constraintType == ConstraintType.boolean) {
113
+ shape[key] = this.buildBooleanSchema(value);
114
+ }
115
+ else if (value.constraintType == ConstraintType.date) {
116
+ shape[key] = this.buildDateSchema(value);
117
+ }
118
+ });
119
+ const schema = object(shape);
120
+ return new ModelValidator(schema);
121
+ }
122
+ buildStringSchema(options) {
123
+ let schema = Yup.string();
124
+ if (options.required) {
125
+ schema = schema.required(options.required.message);
126
+ }
127
+ if (options.length) {
128
+ schema = schema.length(options.length.length, options.length.message);
129
+ }
130
+ if (options.maxLength) {
131
+ schema = schema.max(options.maxLength.maxLength, options.maxLength.message);
132
+ }
133
+ if (options.minLength) {
134
+ schema = schema.min(options.minLength.minLength, options.minLength.message);
135
+ }
136
+ if (options.pattern) {
137
+ schema = schema.matches(options.pattern.pattern, options.pattern.message);
138
+ }
139
+ return schema;
140
+ }
141
+ buildBooleanSchema(options) {
142
+ let schema = Yup.boolean();
143
+ if (options.required) {
144
+ schema = schema.required(options.required.message);
145
+ }
146
+ if (options.equals) {
147
+ if (options.equals.equals) {
148
+ schema = schema.isTrue(options.equals.message);
149
+ }
150
+ else {
151
+ schema = schema.isFalse(options.equals.message);
152
+ }
153
+ }
154
+ return schema;
155
+ }
156
+ buildDateSchema(options) {
157
+ let schema = Yup.date().typeError('Please enter a valid date');
158
+ if (options.required) {
159
+ schema = schema.required(options.required.message);
160
+ }
161
+ if (options.min) {
162
+ schema = schema.min(options.min.min, options.min.message);
163
+ }
164
+ if (options.max) {
165
+ schema = schema.max(options.max.max, options.max.message);
166
+ }
167
+ if (options.test) {
168
+ schema = schema.test({
169
+ name: options.test.name,
170
+ message: options.test.message,
171
+ test: (d, context) => {
172
+ return options.test.test(d);
173
+ }
174
+ });
175
+ }
176
+ return schema;
177
+ }
178
+ }
179
+ ModelSchemaFactory.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
180
+ ModelSchemaFactory.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, providedIn: 'root' });
181
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, decorators: [{
182
+ type: Injectable,
183
+ args: [{
184
+ providedIn: 'root',
185
+ }]
186
+ }], ctorParameters: function () { return []; } });
187
+
188
+ class FieldErrorsComponent {
189
+ get errorMessage() {
190
+ const errorKeys = Object.keys(this.field?.errors || {});
191
+ return errorKeys.length > 0 ? this.field?.errors[errorKeys[0]] : '';
192
+ }
193
+ }
194
+ FieldErrorsComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FieldErrorsComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
195
+ FieldErrorsComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.0.5", type: FieldErrorsComponent, selector: "mz-field-errors", inputs: { field: "field" }, ngImport: i0, template: "<div class=\"field-error\" *ngIf=\"errorMessage\">{{ errorMessage }}</div>\n", styles: [""], dependencies: [{ kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }] });
196
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FieldErrorsComponent, decorators: [{
197
+ type: Component,
198
+ args: [{ selector: 'mz-field-errors', template: "<div class=\"field-error\" *ngIf=\"errorMessage\">{{ errorMessage }}</div>\n" }]
199
+ }], propDecorators: { field: [{
200
+ type: Input
201
+ }] } });
202
+
203
+ class FormsModule {
204
+ }
205
+ FormsModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
206
+ FormsModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, declarations: [FieldErrorsComponent], imports: [CommonModule // TODO: can remove once done with temp error displaying
207
+ ], exports: [FieldErrorsComponent] });
208
+ FormsModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, imports: [CommonModule // TODO: can remove once done with temp error displaying
209
+ ] });
210
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, decorators: [{
211
+ type: NgModule,
212
+ args: [{
213
+ providers: [],
214
+ declarations: [
215
+ FieldErrorsComponent
216
+ ],
217
+ exports: [
218
+ FieldErrorsComponent
219
+ ],
220
+ imports: [
221
+ CommonModule // TODO: can remove once done with temp error displaying
222
+ ]
223
+ }]
224
+ }] });
225
+
226
+ class NgFormModelStateFactory {
227
+ constructor(factory) {
228
+ this.factory = factory;
229
+ }
230
+ create(form, model, options) {
231
+ const modelState = new NgFormModelState(form, this.factory.build(model), model, options);
232
+ return modelState;
233
+ }
234
+ }
235
+ NgFormModelStateFactory.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, deps: [{ token: ModelSchemaFactory }], target: i0.ɵɵFactoryTarget.Injectable });
236
+ NgFormModelStateFactory.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, providedIn: 'root' });
237
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, decorators: [{
238
+ type: Injectable,
239
+ args: [{
240
+ providedIn: 'root',
241
+ }]
242
+ }], ctorParameters: function () { return [{ type: ModelSchemaFactory }]; } });
243
+ class NgFormModelState {
244
+ constructor(form, modelValidator, model, options) {
245
+ this.form = form;
246
+ this.modelValidator = modelValidator;
247
+ this.model = model;
248
+ this.errors = new BehaviorSubject([]);
249
+ this.errors$ = this.errors.asObservable();
250
+ this.options = options;
251
+ this.form.form.valueChanges
252
+ .pipe(switchMap(async (x) => {
253
+ this.model = x;
254
+ return from(this.runValidations(this.options?.onValidate));
255
+ }))
256
+ .subscribe();
257
+ this.errors$.subscribe((list) => {
258
+ const grouped = list.reduce((grouped, v) => grouped.set(v.path, [...(grouped.get(v.path) || []), v]), new Map());
259
+ grouped.forEach((value, key) => {
260
+ let validationErrors = {};
261
+ value.forEach((v) => (validationErrors[v.type] = v.message));
262
+ this.form.controls[key]?.setErrors(validationErrors);
263
+ });
264
+ });
265
+ }
266
+ isValid() {
267
+ return this.errors.value.length == 0;
268
+ }
269
+ setErrors(errors) {
270
+ this.errors.next(errors);
271
+ }
272
+ async validate() {
273
+ return await this.runValidations(this.options?.onValidate);
274
+ }
275
+ async runValidations(callback) {
276
+ this.removeCurrentErrors();
277
+ const list = await this.modelValidator.validate(this.model);
278
+ const final = callback?.(list) || list;
279
+ this.errors.next(final);
280
+ }
281
+ removeCurrentErrors() {
282
+ Object.keys(this.form.controls).forEach(key => this.form.controls[key].setErrors(null));
283
+ }
284
+ }
285
+
286
+ /*
287
+ * Public API Surface of forms
288
+ */
289
+
290
+ /**
291
+ * Generated bundle index. Do not edit.
292
+ */
293
+
294
+ export { BooleanType, ConstraintType, DateType, FieldErrorsComponent, FormsModule, ModelSchemaFactory, ModelValidator, NgFormModelState, NgFormModelStateFactory, StringType, equals, length, max, maxLength, min, minLength, ofValues, pattern, required, test };
295
+ //# sourceMappingURL=muziehdesign-forms.mjs.map