@nsail/stack 0.1.716 → 0.1.723
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/dist/descriptors.d.ts +1 -1
- package/dist/validation.js +23 -5
- package/package.json +1 -1
package/dist/descriptors.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type FieldKind = "string" | "guid" | "boolean" | "integer" | "number" | "date" | "datetime" | "time" | "duration" | "enum" | "array" | "map" | "object" | "unknown";
|
|
1
|
+
export type FieldKind = "string" | "guid" | "boolean" | "integer" | "bigint" | "decimal" | "number" | "date" | "datetime" | "time" | "duration" | "enum" | "array" | "map" | "object" | "unknown";
|
|
2
2
|
export type Binding = "route" | "query" | "header" | "body";
|
|
3
3
|
export type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
|
|
4
4
|
export interface EnumType<TValue extends string = string> {
|
package/dist/validation.js
CHANGED
|
@@ -6,6 +6,12 @@ import { memberOf } from "./descriptors";
|
|
|
6
6
|
const email = /^[^@\s]+@[^@\s]+$/;
|
|
7
7
|
const phone = /^\+?(?=.*\d)[\d\s().\-]+((x|ext\.?)\s?\d+)?$/i;
|
|
8
8
|
const url = /^(https?|ftp):\/\//i;
|
|
9
|
+
// What the server's invariant parse of a long or a decimal takes: blanks around, a sign, no
|
|
10
|
+
// group separator, a point. A value past the type's range passes here and is the server's 400.
|
|
11
|
+
const exact = {
|
|
12
|
+
bigint: /^\s*[+-]?\d+\s*$/,
|
|
13
|
+
decimal: /^\s*[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?\s*$/,
|
|
14
|
+
};
|
|
9
15
|
function issue(code, message, field, args = {}) {
|
|
10
16
|
return { code, message, source: field, arguments: { field, ...args } };
|
|
11
17
|
}
|
|
@@ -18,6 +24,19 @@ function length(value) {
|
|
|
18
24
|
}
|
|
19
25
|
return null;
|
|
20
26
|
}
|
|
27
|
+
// A bigint compares as a BigInt, whole past 2^53; a decimal's text as the Number it rounds to.
|
|
28
|
+
function outside(field, value) {
|
|
29
|
+
if (field.kind === "bigint" && typeof value === "string" && exact.bigint.test(value)) {
|
|
30
|
+
const whole = BigInt(value.trim());
|
|
31
|
+
return ((field.min !== undefined && (Number.isInteger(field.min) ? whole < BigInt(field.min) : Number(whole) < field.min)) ||
|
|
32
|
+
(field.max !== undefined && (Number.isInteger(field.max) ? whole > BigInt(field.max) : Number(whole) > field.max)));
|
|
33
|
+
}
|
|
34
|
+
const quantity = typeof value === "number" ? value : field.kind === "decimal" && typeof value === "string" && value.trim() !== "" ? Number(value) : NaN;
|
|
35
|
+
if (Number.isNaN(quantity)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return (field.min !== undefined && quantity < field.min) || (field.max !== undefined && quantity > field.max);
|
|
39
|
+
}
|
|
21
40
|
export function validateField(field, value) {
|
|
22
41
|
const name = memberOf(field);
|
|
23
42
|
const issues = [];
|
|
@@ -35,13 +54,12 @@ export function validateField(field, value) {
|
|
|
35
54
|
if (field.minLength !== undefined && size !== null && size < field.minLength) {
|
|
36
55
|
issues.push(issue("Invalid", `The field ${name} must have at least ${field.minLength} characters`, name));
|
|
37
56
|
}
|
|
38
|
-
if ((field.min !== undefined || field.max !== undefined) &&
|
|
39
|
-
|
|
40
|
-
issues.push(issue("OutOfRange", `The field ${name} must be between ${field.min} and ${field.max}`, name, { from: String(field.min), to: String(field.max) }));
|
|
41
|
-
}
|
|
57
|
+
if ((field.min !== undefined || field.max !== undefined) && outside(field, value)) {
|
|
58
|
+
issues.push(issue("OutOfRange", `The field ${name} must be between ${field.min} and ${field.max}`, name, { from: String(field.min), to: String(field.max) }));
|
|
42
59
|
}
|
|
43
60
|
if (typeof value === "string" && value.length > 0) {
|
|
44
|
-
const formatted = (field.
|
|
61
|
+
const formatted = (exact[field.kind] !== undefined && !exact[field.kind].test(value)) ||
|
|
62
|
+
(field.format === "email" && !email.test(value)) ||
|
|
45
63
|
(field.format === "phone" && !phone.test(value)) ||
|
|
46
64
|
(field.format === "url" && !url.test(value)) ||
|
|
47
65
|
(field.pattern !== undefined && !new RegExp(`^(?:${field.pattern})$`).test(value));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nsail/stack",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.723",
|
|
4
4
|
"description": "NSail's vendor-free client half: sends messages from the descriptors an Sdk generates, speaks the catalog, and holds the hooks a page reads with.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|