borboleta 0.0.1

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 ADDED
@@ -0,0 +1,64 @@
1
+ # Borboleta
2
+
3
+ This project was generated using [Angular CLI](https://github.com/angular/angular-cli) version 21.2.0.
4
+
5
+ ## Code scaffolding
6
+
7
+ Angular CLI includes powerful code scaffolding tools. To generate a new component, run:
8
+
9
+ ```bash
10
+ ng generate component component-name
11
+ ```
12
+
13
+ For a complete list of available schematics (such as `components`, `directives`, or `pipes`), run:
14
+
15
+ ```bash
16
+ ng generate --help
17
+ ```
18
+
19
+ ## Building
20
+
21
+ To build the library, run:
22
+
23
+ ```bash
24
+ ng build borboleta
25
+ ```
26
+
27
+ This command will compile your project, and the build artifacts will be placed in the `dist/` directory.
28
+
29
+ ### Publishing the Library
30
+
31
+ Once the project is built, you can publish your library by following these steps:
32
+
33
+ 1. Navigate to the `dist` directory:
34
+
35
+ ```bash
36
+ cd dist/borboleta
37
+ ```
38
+
39
+ 2. Run the `npm publish` command to publish your library to the npm registry:
40
+ ```bash
41
+ npm publish
42
+ ```
43
+
44
+ ## Running unit tests
45
+
46
+ To execute unit tests with the [Karma](https://karma-runner.github.io) test runner, use the following command:
47
+
48
+ ```bash
49
+ ng test
50
+ ```
51
+
52
+ ## Running end-to-end tests
53
+
54
+ For end-to-end (e2e) testing, run:
55
+
56
+ ```bash
57
+ ng e2e
58
+ ```
59
+
60
+ Angular CLI does not come with an end-to-end testing framework by default. You can choose one that suits your needs.
61
+
62
+ ## Additional Resources
63
+
64
+ For more information on using the Angular CLI, including detailed command references, visit the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.
@@ -0,0 +1,141 @@
1
+ import { required, validate, email, minLength, maxLength, pattern, min, max, applyEach } from '@angular/forms/signals';
2
+
3
+ /**
4
+ * Converts a JSON Schema (draft-07) definition into an Angular Signal Forms
5
+ * schema function.
6
+ *
7
+ * The returned function can be passed directly to `form()`:
8
+ *
9
+ * ```typescript
10
+ * const model = signal<MyModel>({ ... });
11
+ * const myForm = form(model, toSignalSchema<MyModel>(jsonSchema));
12
+ * ```
13
+ *
14
+ * ### Supported JSON Schema keywords
15
+ *
16
+ * | JSON Schema | Signal Forms validator |
17
+ * |---------------------|------------------------|
18
+ * | `required` | `required()` |
19
+ * | `enum` | `validate()` (custom) |
20
+ * | `minimum`/`maximum` | `min()` / `max()` |
21
+ * | `minLength`/`maxLength` | `minLength()` / `maxLength()` |
22
+ * | `pattern` | `pattern()` |
23
+ * | `format: "email"` | `email()` |
24
+ * | nested `object` | recursive descent |
25
+ * | `items` (array) | `applyEach()` |
26
+ */
27
+ function toSignalSchema(jsonSchema) {
28
+ return (schemaPath) => {
29
+ applyObjectValidators(schemaPath, jsonSchema);
30
+ };
31
+ }
32
+ // ---------------------------------------------------------------------------
33
+ // Internal helpers
34
+ // ---------------------------------------------------------------------------
35
+ function applyObjectValidators(currentPath, schema) {
36
+ if (!schema.properties)
37
+ return;
38
+ const requiredFields = new Set(schema.required ?? []);
39
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
40
+ const fieldPath = currentPath[key];
41
+ if (requiredFields.has(key)) {
42
+ required(fieldPath);
43
+ }
44
+ applyFieldValidators(fieldPath, propSchema);
45
+ }
46
+ }
47
+ function applyFieldValidators(fieldPath, schema) {
48
+ // Enum constraint — works for any type
49
+ if (schema.enum != null) {
50
+ const allowed = schema.enum;
51
+ validate(fieldPath, ({ value }) => {
52
+ const v = value();
53
+ if (v != null && v !== '' && !allowed.includes(v)) {
54
+ return { kind: 'enum', message: `Must be one of: ${allowed.join(', ')}` };
55
+ }
56
+ return null;
57
+ });
58
+ }
59
+ // String-specific validations
60
+ if (schema.type === 'string') {
61
+ if (schema.format === 'email') {
62
+ email(fieldPath);
63
+ }
64
+ if (schema.minLength != null) {
65
+ minLength(fieldPath, schema.minLength);
66
+ }
67
+ if (schema.maxLength != null) {
68
+ maxLength(fieldPath, schema.maxLength);
69
+ }
70
+ if (schema.pattern != null) {
71
+ pattern(fieldPath, new RegExp(schema.pattern));
72
+ }
73
+ }
74
+ // Number / integer validations
75
+ if (schema.type === 'number' || schema.type === 'integer') {
76
+ if (schema.minimum != null) {
77
+ min(fieldPath, schema.minimum);
78
+ }
79
+ if (schema.maximum != null) {
80
+ max(fieldPath, schema.maximum);
81
+ }
82
+ }
83
+ // Nested object — recurse
84
+ if (schema.type === 'object' && schema.properties) {
85
+ applyObjectValidators(fieldPath, schema);
86
+ }
87
+ // Array items — recurse with applyEach
88
+ if (schema.type === 'array' && schema.items && !Array.isArray(schema.items)) {
89
+ const itemSchema = schema.items;
90
+ applyEach(fieldPath, ((itemPath) => {
91
+ if (itemSchema.type === 'object') {
92
+ applyObjectValidators(itemPath, itemSchema);
93
+ }
94
+ else {
95
+ applyFieldValidators(itemPath, itemSchema);
96
+ }
97
+ }));
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Combines multiple schema functions into one.
103
+ *
104
+ * Each schema function is called in order on the same `SchemaPathTree`,
105
+ * so validators from all schemas accumulate on the form.
106
+ *
107
+ * ```typescript
108
+ * const myForm = form(
109
+ * model,
110
+ * composeSchemas<MyModel>(
111
+ * toSignalSchema(jsonSchema),
112
+ * (s) => {
113
+ * validate(s.run.url, ({ value }) => {
114
+ * if (!value().startsWith('https://')) {
115
+ * return { kind: 'https', message: 'URL must use HTTPS' };
116
+ * }
117
+ * return null;
118
+ * });
119
+ * },
120
+ * ),
121
+ * );
122
+ * ```
123
+ */
124
+ function composeSchemas(...schemas) {
125
+ return (schemaPath) => {
126
+ for (const schema of schemas) {
127
+ schema(schemaPath);
128
+ }
129
+ };
130
+ }
131
+
132
+ /*
133
+ * Public API Surface of borboleta
134
+ */
135
+
136
+ /**
137
+ * Generated bundle index. Do not edit.
138
+ */
139
+
140
+ export { composeSchemas, toSignalSchema };
141
+ //# sourceMappingURL=borboleta.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"borboleta.mjs","sources":["../../../projects/borboleta/src/lib/to-signal-schema.ts","../../../projects/borboleta/src/lib/compose-schemas.ts","../../../projects/borboleta/src/public-api.ts","../../../projects/borboleta/src/borboleta.ts"],"sourcesContent":["import {\n required,\n validate,\n pattern as patternValidator,\n min,\n max,\n minLength,\n maxLength,\n email,\n applyEach,\n} from '@angular/forms/signals';\nimport type { SchemaFn } from '@angular/forms/signals';\nimport type { JsonSchema } from './json-schema.types';\n\n/**\n * Converts a JSON Schema (draft-07) definition into an Angular Signal Forms\n * schema function.\n *\n * The returned function can be passed directly to `form()`:\n *\n * ```typescript\n * const model = signal<MyModel>({ ... });\n * const myForm = form(model, toSignalSchema<MyModel>(jsonSchema));\n * ```\n *\n * ### Supported JSON Schema keywords\n *\n * | JSON Schema | Signal Forms validator |\n * |---------------------|------------------------|\n * | `required` | `required()` |\n * | `enum` | `validate()` (custom) |\n * | `minimum`/`maximum` | `min()` / `max()` |\n * | `minLength`/`maxLength` | `minLength()` / `maxLength()` |\n * | `pattern` | `pattern()` |\n * | `format: \"email\"` | `email()` |\n * | nested `object` | recursive descent |\n * | `items` (array) | `applyEach()` |\n */\nexport function toSignalSchema<T>(jsonSchema: JsonSchema): SchemaFn<T> {\n return (schemaPath) => {\n applyObjectValidators(schemaPath as any, jsonSchema);\n };\n}\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\nfunction applyObjectValidators(currentPath: any, schema: JsonSchema): void {\n if (!schema.properties) return;\n\n const requiredFields = new Set(schema.required ?? []);\n\n for (const [key, propSchema] of Object.entries(schema.properties)) {\n const fieldPath = currentPath[key];\n\n if (requiredFields.has(key)) {\n required(fieldPath);\n }\n\n applyFieldValidators(fieldPath, propSchema);\n }\n}\n\nfunction applyFieldValidators(fieldPath: any, schema: JsonSchema): void {\n // Enum constraint — works for any type\n if (schema.enum != null) {\n const allowed = schema.enum;\n validate(fieldPath, ({ value }) => {\n const v = value();\n if (v != null && v !== '' && !allowed.includes(v as never)) {\n return { kind: 'enum', message: `Must be one of: ${allowed.join(', ')}` };\n }\n return null;\n });\n }\n\n // String-specific validations\n if (schema.type === 'string') {\n if (schema.format === 'email') {\n email(fieldPath);\n }\n if (schema.minLength != null) {\n minLength(fieldPath, schema.minLength);\n }\n if (schema.maxLength != null) {\n maxLength(fieldPath, schema.maxLength);\n }\n if (schema.pattern != null) {\n patternValidator(fieldPath, new RegExp(schema.pattern));\n }\n }\n\n // Number / integer validations\n if (schema.type === 'number' || schema.type === 'integer') {\n if (schema.minimum != null) {\n min(fieldPath, schema.minimum);\n }\n if (schema.maximum != null) {\n max(fieldPath, schema.maximum);\n }\n }\n\n // Nested object — recurse\n if (schema.type === 'object' && schema.properties) {\n applyObjectValidators(fieldPath, schema);\n }\n\n // Array items — recurse with applyEach\n if (schema.type === 'array' && schema.items && !Array.isArray(schema.items)) {\n const itemSchema = schema.items;\n applyEach(fieldPath, ((itemPath: any) => {\n if (itemSchema.type === 'object') {\n applyObjectValidators(itemPath, itemSchema);\n } else {\n applyFieldValidators(itemPath, itemSchema);\n }\n }) as any);\n }\n}\n","import type { SchemaFn } from '@angular/forms/signals';\n\n/**\n * Combines multiple schema functions into one.\n *\n * Each schema function is called in order on the same `SchemaPathTree`,\n * so validators from all schemas accumulate on the form.\n *\n * ```typescript\n * const myForm = form(\n * model,\n * composeSchemas<MyModel>(\n * toSignalSchema(jsonSchema),\n * (s) => {\n * validate(s.run.url, ({ value }) => {\n * if (!value().startsWith('https://')) {\n * return { kind: 'https', message: 'URL must use HTTPS' };\n * }\n * return null;\n * });\n * },\n * ),\n * );\n * ```\n */\nexport function composeSchemas<T>(...schemas: SchemaFn<T>[]): SchemaFn<T> {\n return (schemaPath) => {\n for (const schema of schemas) {\n schema(schemaPath);\n }\n };\n}\n","/*\n * Public API Surface of borboleta\n */\n\nexport { toSignalSchema } from './lib/to-signal-schema';\nexport { composeSchemas } from './lib/compose-schemas';\nexport type { JsonSchema } from './lib/json-schema.types';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport *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M,UAAU,GAAG,MAAM,CAAC,KAAK;AAC/B,QAAA,SAAS,CAAC,SAAS,GAAG,CAAC,QAAa,KAAI;AACtC,YAAA,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE;AAChC,gBAAA,qBAAqB,CAAC,QAAQ,EAAE,UAAU,CAAC;YAC7C;iBAAO;AACL,gBAAA,oBAAoB,CAAC,QAAQ,EAAE,UAAU,CAAC;YAC5C;QACF,CAAC,EAAS;IACZ;AACF;;ACrHA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,SAAU,cAAc,CAAI,GAAG,OAAsB,EAAA;IACzD,OAAO,CAAC,UAAU,KAAI;AACpB,QAAA,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;YAC5B,MAAM,CAAC,UAAU,CAAC;QACpB;AACF,IAAA,CAAC;AACH;;AC/BA;;AAEG;;ACFH;;AAEG;;;;"}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "borboleta",
3
+ "version": "0.0.1",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/easyware-io/borboleta"
7
+ },
8
+ "peerDependencies": {
9
+ "@angular/common": "^21.2.0",
10
+ "@angular/core": "^21.2.0",
11
+ "@angular/forms": "^21.2.0"
12
+ },
13
+ "dependencies": {
14
+ "tslib": "^2.3.0"
15
+ },
16
+ "sideEffects": false,
17
+ "module": "fesm2022/borboleta.mjs",
18
+ "typings": "types/borboleta.d.ts",
19
+ "exports": {
20
+ "./package.json": {
21
+ "default": "./package.json"
22
+ },
23
+ ".": {
24
+ "types": "./types/borboleta.d.ts",
25
+ "default": "./fesm2022/borboleta.mjs"
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,80 @@
1
+ import { SchemaFn } from '@angular/forms/signals';
2
+
3
+ /**
4
+ * Subset of JSON Schema Draft-07 that Borboleta can convert into
5
+ * Angular Signal Forms schema functions.
6
+ */
7
+ interface JsonSchema {
8
+ $schema?: string;
9
+ title?: string;
10
+ description?: string;
11
+ type?: string;
12
+ properties?: Record<string, JsonSchema>;
13
+ required?: string[];
14
+ enum?: unknown[];
15
+ minimum?: number;
16
+ maximum?: number;
17
+ exclusiveMinimum?: number;
18
+ exclusiveMaximum?: number;
19
+ minLength?: number;
20
+ maxLength?: number;
21
+ pattern?: string;
22
+ format?: string;
23
+ items?: JsonSchema;
24
+ additionalProperties?: boolean | JsonSchema;
25
+ default?: unknown;
26
+ }
27
+
28
+ /**
29
+ * Converts a JSON Schema (draft-07) definition into an Angular Signal Forms
30
+ * schema function.
31
+ *
32
+ * The returned function can be passed directly to `form()`:
33
+ *
34
+ * ```typescript
35
+ * const model = signal<MyModel>({ ... });
36
+ * const myForm = form(model, toSignalSchema<MyModel>(jsonSchema));
37
+ * ```
38
+ *
39
+ * ### Supported JSON Schema keywords
40
+ *
41
+ * | JSON Schema | Signal Forms validator |
42
+ * |---------------------|------------------------|
43
+ * | `required` | `required()` |
44
+ * | `enum` | `validate()` (custom) |
45
+ * | `minimum`/`maximum` | `min()` / `max()` |
46
+ * | `minLength`/`maxLength` | `minLength()` / `maxLength()` |
47
+ * | `pattern` | `pattern()` |
48
+ * | `format: "email"` | `email()` |
49
+ * | nested `object` | recursive descent |
50
+ * | `items` (array) | `applyEach()` |
51
+ */
52
+ declare function toSignalSchema<T>(jsonSchema: JsonSchema): SchemaFn<T>;
53
+
54
+ /**
55
+ * Combines multiple schema functions into one.
56
+ *
57
+ * Each schema function is called in order on the same `SchemaPathTree`,
58
+ * so validators from all schemas accumulate on the form.
59
+ *
60
+ * ```typescript
61
+ * const myForm = form(
62
+ * model,
63
+ * composeSchemas<MyModel>(
64
+ * toSignalSchema(jsonSchema),
65
+ * (s) => {
66
+ * validate(s.run.url, ({ value }) => {
67
+ * if (!value().startsWith('https://')) {
68
+ * return { kind: 'https', message: 'URL must use HTTPS' };
69
+ * }
70
+ * return null;
71
+ * });
72
+ * },
73
+ * ),
74
+ * );
75
+ * ```
76
+ */
77
+ declare function composeSchemas<T>(...schemas: SchemaFn<T>[]): SchemaFn<T>;
78
+
79
+ export { composeSchemas, toSignalSchema };
80
+ export type { JsonSchema };