@empyria/common 0.1.6 → 0.1.7
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 +8 -8
- package/lib/Ata.js +34 -0
- package/package.json +1 -1
- package/test/Ata.test.js +27 -0
package/README.md
CHANGED
|
@@ -41,14 +41,14 @@ import { defineSchema } from '@empyria/common/lib/Ata.js'
|
|
|
41
41
|
|
|
42
42
|
## Modules
|
|
43
43
|
|
|
44
|
-
| Module | Purpose
|
|
45
|
-
| -------------------------------------- |
|
|
46
|
-
| [index.js](./index.js) | Package entry point; re-exports every module below plus Moleculer meta helpers (`moleculerMeta`, `moleculerEmpyriaMeta`) and workflow ID helpers (`workflowName`, `workflowId`, `identifyWorkflow`).
|
|
47
|
-
| [lib/Errors.js](./lib/Errors.js) | `EmpyriaError`, the `ErrorCreator` factory, and `BaseErrors` — Empyria's catalog of pre-built, templated errors.
|
|
48
|
-
| [lib/Env.js](./lib/Env.js) | `createEnv` — builds a service's environment-variable JSON schema from Empyria-wide defaults.
|
|
49
|
-
| [lib/Ata.js](./lib/Ata.js) | JSON schema building blocks (`string`, `number`, `bool`, `enumType`, `email`, `password`, `ip4`, `ip6`, ...) and validation (`defineSchema`, `createValidator`, `validate`)
|
|
50
|
-
| [lib/Codogram.js](./lib/Codogram.js) | `generateCodogram` — one-time challenge codes (PIN, alphanumeric, alphabetic) for SMS/TOTP-style verification flows.
|
|
51
|
-
| [lib/Defaulter.js](./lib/Defaulter.js) | `defaultsFromSchema` — builds a fully-populated default object for a JSON schema.
|
|
44
|
+
| Module | Purpose |
|
|
45
|
+
| -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
46
|
+
| [index.js](./index.js) | Package entry point; re-exports every module below plus Moleculer meta helpers (`moleculerMeta`, `moleculerEmpyriaMeta`) and workflow ID helpers (`workflowName`, `workflowId`, `identifyWorkflow`). |
|
|
47
|
+
| [lib/Errors.js](./lib/Errors.js) | `EmpyriaError`, the `ErrorCreator` factory, and `BaseErrors` — Empyria's catalog of pre-built, templated errors. |
|
|
48
|
+
| [lib/Env.js](./lib/Env.js) | `createEnv` — builds a service's environment-variable JSON schema from Empyria-wide defaults. |
|
|
49
|
+
| [lib/Ata.js](./lib/Ata.js) | JSON schema building blocks (`string`, `number`, `bool`, `enumType`, `email`, `password`, `ip4`, `ip6`, ...) and validation — throwing (`defineSchema`, `createValidator`, `validate`) or non-throwing/structured (`createChecker`, `checkSchema`) — built on [ata-validator](https://www.npmjs.com/package/ata-validator). |
|
|
50
|
+
| [lib/Codogram.js](./lib/Codogram.js) | `generateCodogram` — one-time challenge codes (PIN, alphanumeric, alphabetic) for SMS/TOTP-style verification flows. |
|
|
51
|
+
| [lib/Defaulter.js](./lib/Defaulter.js) | `defaultsFromSchema` — builds a fully-populated default object for a JSON schema. |
|
|
52
52
|
|
|
53
53
|
Every exported function is documented with JSDoc directly in its source file — hovering
|
|
54
54
|
a function in VSCode or Zed shows its parameters and return type without any extra
|
package/lib/Ata.js
CHANGED
|
@@ -118,6 +118,40 @@ export function validate(schema, input, options) {
|
|
|
118
118
|
return validatedInput
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Builds a non-throwing checking function for a JSON schema — the structured-result
|
|
123
|
+
* counterpart to {@link createValidator}. Use it when a caller needs `ata-validator`'s own
|
|
124
|
+
* error list (to build its own error type, count violations, or render them itself with
|
|
125
|
+
* {@link renderCompact} / {@link renderJSON}) rather than a `BaseErrors.ValidationError`
|
|
126
|
+
* carrying a pre-rendered string.
|
|
127
|
+
* @param {Object} schema - JSON schema to validate against.
|
|
128
|
+
* @param {Object} [options] - Forwarded to `ata-validator`'s `Validator` constructor
|
|
129
|
+
* (see {@link createValidator} for the common flags).
|
|
130
|
+
* @returns {(data: *) => { valid: boolean, errors: Array<Object> }} Function that returns the
|
|
131
|
+
* verdict and, when invalid, `ata-validator`'s raw error objects (empty array when valid).
|
|
132
|
+
* Never throws on a validation failure.
|
|
133
|
+
*/
|
|
134
|
+
export function createChecker(schema, options) {
|
|
135
|
+
const v = new Validator(schema, options)
|
|
136
|
+
|
|
137
|
+
return (data) => {
|
|
138
|
+
const result = v.validate(data)
|
|
139
|
+
return { valid: result.valid, errors: result.valid ? [] : result.errors }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Checks `input` against `schema` in one call, without throwing.
|
|
145
|
+
* @param {Object} schema - JSON schema to validate against.
|
|
146
|
+
* @param {*} input - Value to validate.
|
|
147
|
+
* @param {Object} [options] - Forwarded to {@link createChecker} (see its docs).
|
|
148
|
+
* @returns {{ valid: boolean, errors: Array<Object> }} The verdict, plus `ata-validator`'s raw
|
|
149
|
+
* error objects when invalid (empty array when valid).
|
|
150
|
+
*/
|
|
151
|
+
export function checkSchema(schema, input, options) {
|
|
152
|
+
return createChecker(schema, options)(input)
|
|
153
|
+
}
|
|
154
|
+
|
|
121
155
|
/**
|
|
122
156
|
* Re-exported from `ata-validator`:
|
|
123
157
|
* - `renderCompact(errors)` renders a validation error list compactly (one line each).
|
package/package.json
CHANGED
package/test/Ata.test.js
CHANGED
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
cloneBySchema,
|
|
8
8
|
defineSchema,
|
|
9
9
|
validate,
|
|
10
|
+
createChecker,
|
|
11
|
+
checkSchema,
|
|
10
12
|
bool,
|
|
11
13
|
string,
|
|
12
14
|
enumType,
|
|
@@ -183,3 +185,28 @@ describe('renderCompact / renderJSON re-exports', () => {
|
|
|
183
185
|
expect(typeof renderJSON).toBe('function')
|
|
184
186
|
})
|
|
185
187
|
})
|
|
188
|
+
|
|
189
|
+
describe('createChecker / checkSchema', () => {
|
|
190
|
+
const schema = defineSchema({ name: string() })
|
|
191
|
+
|
|
192
|
+
test('checkSchema returns valid:true and no errors for good input', () => {
|
|
193
|
+
expect(checkSchema(schema, { name: 'Bob' })).toEqual({ valid: true, errors: [] })
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test('checkSchema returns valid:false and a non-empty error list for bad input, without throwing', () => {
|
|
197
|
+
const result = checkSchema(schema, {})
|
|
198
|
+
expect(result.valid).toBe(false)
|
|
199
|
+
expect(Array.isArray(result.errors)).toBe(true)
|
|
200
|
+
expect(result.errors.length).toBeGreaterThan(0)
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test('checkSchema errors render through the re-exported renderCompact', () => {
|
|
204
|
+
const { errors } = checkSchema(schema, {})
|
|
205
|
+
expect(renderCompact(errors)).toContain('name')
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test('createChecker builds a reusable checker and forwards options (coerceTypes)', () => {
|
|
209
|
+
const check = createChecker(defineSchema({ port: number() }), { coerceTypes: true })
|
|
210
|
+
expect(check({ port: '4040' })).toEqual({ valid: true, errors: [] })
|
|
211
|
+
})
|
|
212
|
+
})
|