mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-01-23 07:51:18 +01:00
Refactor TavernCardValidator error property
This commit is contained in:
parent
a876d098fe
commit
42d838a0b3
@ -1188,7 +1188,8 @@ app.post("/v2/editcharacterattribute", jsonParser, async function (request, resp
|
|||||||
character = deepMerge(character, update);
|
character = deepMerge(character, update);
|
||||||
|
|
||||||
const validator = new TavernCardValidator(character);
|
const validator = new TavernCardValidator(character);
|
||||||
if (validator.validateV2()) {
|
|
||||||
|
if (true === validator.validateV2()) {
|
||||||
await charaWrite(
|
await charaWrite(
|
||||||
avatarPath,
|
avatarPath,
|
||||||
JSON.stringify(character),
|
JSON.stringify(character),
|
||||||
@ -1197,7 +1198,7 @@ app.post("/v2/editcharacterattribute", jsonParser, async function (request, resp
|
|||||||
'Character saved'
|
'Character saved'
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
response.status(400).send({message: `Validation failed for card ${character.name}`, field: validator.getValidationError});
|
response.status(400).send({message: `Validation failed for card ${character.name}`, field: validator.lastValidationError});
|
||||||
}
|
}
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
response.status(500).send({message: 'Unexpected error while saving character.', error: exception.toString()});
|
response.status(500).send({message: 'Unexpected error while saving character.', error: exception.toString()});
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
const characterBook = require("lodash/object");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the data structure of character cards.
|
* Validates the data structure of character cards.
|
||||||
* Supported specs: V1, V2
|
* Supported specs: V1, V2
|
||||||
@ -8,7 +6,7 @@ const characterBook = require("lodash/object");
|
|||||||
* @link https://github.com/malfoyslastname/character-card-spec-v2
|
* @link https://github.com/malfoyslastname/character-card-spec-v2
|
||||||
*/
|
*/
|
||||||
class TavernCardValidator {
|
class TavernCardValidator {
|
||||||
#validationError = null;
|
#lastValidationError = null;
|
||||||
|
|
||||||
constructor(card) {
|
constructor(card) {
|
||||||
this.card = card;
|
this.card = card;
|
||||||
@ -19,24 +17,24 @@ class TavernCardValidator {
|
|||||||
*
|
*
|
||||||
* @returns {null|string}
|
* @returns {null|string}
|
||||||
*/
|
*/
|
||||||
get validationError() {
|
get lastValidationError() {
|
||||||
return this.#validationError;
|
return this.#lastValidationError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate against V1 and V2 spec.
|
* Validate against V1 or V2 spec.
|
||||||
*
|
*
|
||||||
* @returns {number|boolean} - false when neither V1 nor V2 spec were matched. Specification version number otherwise.
|
* @returns {number|boolean} - false when neither V1 nor V2 spec were matched. Specification version number otherwise.
|
||||||
*/
|
*/
|
||||||
validate() {
|
validate() {
|
||||||
this.#validationError = null;
|
this.#lastValidationError = null;
|
||||||
|
|
||||||
if (this.validateV1()) {
|
if (this.validateV1()) {
|
||||||
return 2;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.validateV2()) {
|
if (this.validateV2()) {
|
||||||
return 1;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -51,7 +49,7 @@ class TavernCardValidator {
|
|||||||
const requiredFields = ['name', 'description', 'personality', 'scenario', 'first_mes', 'mes_example'];
|
const requiredFields = ['name', 'description', 'personality', 'scenario', 'first_mes', 'mes_example'];
|
||||||
return requiredFields.every(field => {
|
return requiredFields.every(field => {
|
||||||
if (!this.card.hasOwnProperty(field)) {
|
if (!this.card.hasOwnProperty(field)) {
|
||||||
this.#validationError = field;
|
this.#lastValidationError = field;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -72,7 +70,7 @@ class TavernCardValidator {
|
|||||||
|
|
||||||
#validateSpec() {
|
#validateSpec() {
|
||||||
if (this.card.spec !== 'chara_card_v2') {
|
if (this.card.spec !== 'chara_card_v2') {
|
||||||
this.#validationError = 'spec';
|
this.#lastValidationError = 'spec';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -80,7 +78,7 @@ class TavernCardValidator {
|
|||||||
|
|
||||||
#validateSpecVersion() {
|
#validateSpecVersion() {
|
||||||
if (this.card.spec_version !== '2.0') {
|
if (this.card.spec_version !== '2.0') {
|
||||||
this.#validationError = 'spec_version';
|
this.#lastValidationError = 'spec_version';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -90,14 +88,14 @@ class TavernCardValidator {
|
|||||||
const data = this.card.data;
|
const data = this.card.data;
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
this.#validationError = 'data';
|
this.#lastValidationError = 'data';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const requiredFields = ['name', 'description', 'personality', 'scenario', 'first_mes', 'mes_example', 'creator_notes', 'system_prompt', 'post_history_instructions', 'alternate_greetings', 'tags', 'creator', 'character_version', 'extensions'];
|
const requiredFields = ['name', 'description', 'personality', 'scenario', 'first_mes', 'mes_example', 'creator_notes', 'system_prompt', 'post_history_instructions', 'alternate_greetings', 'tags', 'creator', 'character_version', 'extensions'];
|
||||||
const isAllRequiredFieldsPresent = requiredFields.every(field => {
|
const isAllRequiredFieldsPresent = requiredFields.every(field => {
|
||||||
if (!data.hasOwnProperty(field)) {
|
if (!data.hasOwnProperty(field)) {
|
||||||
this.#validationError = `data.${field}`;
|
this.#lastValidationError = `data.${field}`;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -116,7 +114,7 @@ class TavernCardValidator {
|
|||||||
const requiredFields = ['extensions', 'entries'];
|
const requiredFields = ['extensions', 'entries'];
|
||||||
const isAllRequiredFieldsPresent = requiredFields.every(field => {
|
const isAllRequiredFieldsPresent = requiredFields.every(field => {
|
||||||
if (!characterBook.hasOwnProperty(field)) {
|
if (!characterBook.hasOwnProperty(field)) {
|
||||||
this.#validationError = `data.character_book.${field}`;
|
this.#lastValidationError = `data.character_book.${field}`;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user