schemaglobin
Advanced tools
Comparing version 5.3.0 to 5.4.0
/** | ||
* Returned from `validate()` when a value is invalid. | ||
* Message is a status message that is safe to show to users. | ||
* - Basic message is neither good nor bad — should be extended to indicate a more-specific status. | ||
* | ||
* @param message String message that is safe to show to users. | ||
* @param details Set of other `Message` instances describing the issue in further detail. | ||
*/ | ||
export declare class Invalid { | ||
export declare class Message { | ||
readonly message: string; | ||
readonly messages?: { | ||
[name: string]: string; | ||
}; | ||
constructor(message: string, messages?: { | ||
[name: string]: string; | ||
}); | ||
readonly details?: Record<string, Message>; | ||
constructor(message: string, details?: Record<string, Message>); | ||
/** Convert the children of this `Message` into an object in `{ name: message }` format. */ | ||
get messages(): Record<string, string>; | ||
/** | ||
* Convert to string. | ||
* Includes the message and the messages under this, e.g. | ||
* Convert to string (equivalent to `message.details`). | ||
* Returns a string including the main message string and a deeply nested array of child message strings. | ||
* | ||
* Invalid format | ||
* name: Must be string | ||
* age: Must be number | ||
* > Invalid format | ||
* > - name: Invalid format | ||
* > - first: Must be string | ||
* > - last: Must be string | ||
* > - age: Must be number | ||
*/ | ||
toString(): string; | ||
/** Convert a Message to a JSON format. */ | ||
toJSON(): string | Record<string, unknown>; | ||
/** Create a Message from its corresponding JSON format. */ | ||
static fromJSON(json: unknown): Message; | ||
} | ||
/** Specific type of Invalid that is returned when a value is required. */ | ||
/** Specific type of `Message` returned from `validate()` when a value is invalid. */ | ||
export declare class Invalid extends Message { | ||
} | ||
/** Specific type of `Invalid` returned when a value is required. */ | ||
export declare class Required extends Invalid { | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Required = exports.Invalid = void 0; | ||
exports.Required = exports.Invalid = exports.Message = void 0; | ||
const object_1 = require("./object"); | ||
const entryToString = ([key, message]) => `- ${key}: ${message.toString().replace("\n", "\n\t\t")}`; | ||
const messageToString = (message) => message.message; | ||
const messageToJSON = (message) => message.toJSON(); | ||
/** | ||
* Returned from `validate()` when a value is invalid. | ||
* Message is a status message that is safe to show to users. | ||
* - Basic message is neither good nor bad — should be extended to indicate a more-specific status. | ||
* | ||
* @param message String message that is safe to show to users. | ||
* @param details Set of other `Message` instances describing the issue in further detail. | ||
*/ | ||
class Invalid { | ||
constructor(message, messages) { | ||
class Message { | ||
constructor(message, details) { | ||
this.message = message; | ||
this.messages = messages; | ||
this.details = details; | ||
} | ||
/** Convert the children of this `Message` into an object in `{ name: message }` format. */ | ||
get messages() { | ||
return this.details ? object_1.mapObject(this.details, messageToString) : {}; | ||
} | ||
/** | ||
* Convert to string. | ||
* Includes the message and the messages under this, e.g. | ||
* Convert to string (equivalent to `message.details`). | ||
* Returns a string including the main message string and a deeply nested array of child message strings. | ||
* | ||
* Invalid format | ||
* name: Must be string | ||
* age: Must be number | ||
* > Invalid format | ||
* > - name: Invalid format | ||
* > - first: Must be string | ||
* > - last: Must be string | ||
* > - age: Must be number | ||
*/ | ||
toString() { | ||
if (!this.messages) | ||
const messages = this.details ? Object.entries(this.details).map(entryToString).join("\n") : ""; | ||
return `${this.message}${messages ? `\n${messages}` : ""}`; | ||
} | ||
/** Convert a Message to a JSON format. */ | ||
toJSON() { | ||
if (!this.details) | ||
return this.message; | ||
const messages = Object.entries(this.messages) | ||
.map(([key, value]) => `- ${key}: ${value}`) | ||
.join("\n"); | ||
return `${this.message}\n${messages}`; | ||
return { | ||
message: this.message, | ||
details: object_1.mapObject(this.details, messageToJSON), | ||
}; | ||
} | ||
/** Create a Message from its corresponding JSON format. */ | ||
static fromJSON(json) { | ||
// String format is simple. | ||
if (typeof json === "string") | ||
return new Message(json); | ||
// Object format is slightly more compled. | ||
if (object_1.isObject(json) && typeof json.message === "string") | ||
return new Message(json.message, object_1.isObject(json.details) ? object_1.mapObject(json.details, Message.fromJSON) : undefined); | ||
// Anything else is impossible. | ||
throw new Error("Incorrect JSON format"); | ||
} | ||
} | ||
exports.Message = Message; | ||
/** Specific type of `Message` returned from `validate()` when a value is invalid. */ | ||
class Invalid extends Message { | ||
} | ||
exports.Invalid = Invalid; | ||
/** Specific type of Invalid that is returned when a value is required. */ | ||
/** Specific type of `Invalid` returned when a value is required. */ | ||
class Required extends Invalid { | ||
} | ||
exports.Required = Required; |
@@ -10,3 +10,3 @@ "use strict"; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
@@ -13,0 +13,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -86,3 +86,3 @@ "use strict"; | ||
invalid = true; | ||
invalids[i.toString()] = value.message; | ||
invalids[i.toString()] = value; | ||
} | ||
@@ -89,0 +89,0 @@ else { |
@@ -65,3 +65,3 @@ "use strict"; | ||
invalid = true; | ||
invalids[key] = safeProp.message; | ||
invalids[key] = safeProp; | ||
} | ||
@@ -76,3 +76,3 @@ else { | ||
if (invalid) | ||
return new invalid_1.Invalid("Invalid format", invalids); | ||
return new invalid_1.Invalid("Invalid items", invalids); | ||
// Return immuatably (return output if changes were made, or exact input otherwise). | ||
@@ -79,0 +79,0 @@ return (changed ? output : unsafeObject); |
@@ -54,3 +54,3 @@ "use strict"; | ||
invalid = true; | ||
invalids[key] = safeProp.message; | ||
invalids[key] = safeProp; | ||
} | ||
@@ -57,0 +57,0 @@ else { |
{ | ||
"name": "schemaglobin", | ||
"description": "Validate user-entered data.", | ||
"version": "5.3.0", | ||
"version": "5.4.0", | ||
"repository": "https://github.com/dhoulb/schemaglobin", | ||
@@ -30,14 +30,14 @@ "author": "Dave Houlbrooke <dave@shax.com>", | ||
"devDependencies": { | ||
"@types/jest": "^26.0.9", | ||
"@typescript-eslint/eslint-plugin": "^3.8.0", | ||
"@typescript-eslint/parser": "^3.8.0", | ||
"eslint": "^7.6.0", | ||
"@types/jest": "^26.0.10", | ||
"@typescript-eslint/eslint-plugin": "^3.9.1", | ||
"@typescript-eslint/parser": "^3.9.1", | ||
"eslint": "^7.7.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"jest": "^26.2.2", | ||
"jest": "^26.4.2", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^26.1.4", | ||
"typescript": "^3.9.7" | ||
"ts-jest": "^26.2.0", | ||
"typescript": "^4.0.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
152450
2841