Spaces:
Running
Running
File size: 4,597 Bytes
1307964 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
/**
* Validates the data structure of character cards.
* Supported specs: V1, V2
* Up to: 8083fb3
*
* @link https://github.com/malfoyslastname/character-card-spec-v2
*/
class TavernCardValidator {
/**
* @type {string|null}
*/
#lastValidationError = null;
constructor(card) {
this.card = card;
}
/**
* Field that caused the validation to fail
*
* @returns {null|string}
*/
get lastValidationError() {
return this.#lastValidationError;
}
/**
* Validate against V1 or V2 spec.
*
* @returns {number|boolean} - false when neither V1 nor V2 spec were matched. Specification version number otherwise.
*/
validate() {
this.#lastValidationError = null;
if (this.validateV1()) {
return 1;
}
if (this.validateV2()) {
return 2;
}
if (this.validateV3()) {
return 3;
}
return false;
}
/**
* Validate against V1 specification
*
* @returns {this is string[]}
*/
validateV1() {
const requiredFields = ['name', 'description', 'personality', 'scenario', 'first_mes', 'mes_example'];
return requiredFields.every(field => {
if (!Object.hasOwn(this.card, field)) {
this.#lastValidationError = field;
return false;
}
return true;
});
}
/**
* Validate against V2 specification
*
* @returns {false|boolean|*}
*/
validateV2() {
return this.#validateSpecV2()
&& this.#validateSpecVersionV2()
&& this.#validateDataV2()
&& this.#validateCharacterBookV2();
}
/**
* Validate against V3 specification
* @returns {boolean}
*/
validateV3() {
return this.#validateSpecV3()
&& this.#validateSpecVersionV3()
&& this.#validateDataV3();
}
#validateSpecV2() {
if (this.card.spec !== 'chara_card_v2') {
this.#lastValidationError = 'spec';
return false;
}
return true;
}
#validateSpecVersionV2() {
if (this.card.spec_version !== '2.0') {
this.#lastValidationError = 'spec_version';
return false;
}
return true;
}
#validateDataV2() {
const data = this.card.data;
if (!data) {
this.#lastValidationError = 'No tavern card data found';
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 isAllRequiredFieldsPresent = requiredFields.every(field => {
if (!Object.hasOwn(data, field)) {
this.#lastValidationError = `data.${field}`;
return false;
}
return true;
});
return isAllRequiredFieldsPresent && Array.isArray(data.alternate_greetings) && Array.isArray(data.tags) && typeof data.extensions === 'object';
}
#validateCharacterBookV2() {
const characterBook = this.card.data.character_book;
if (!characterBook) {
return true;
}
const requiredFields = ['extensions', 'entries'];
const isAllRequiredFieldsPresent = requiredFields.every(field => {
if (!Object.hasOwn(characterBook, field)) {
this.#lastValidationError = `data.character_book.${field}`;
return false;
}
return true;
});
return isAllRequiredFieldsPresent && Array.isArray(characterBook.entries) && typeof characterBook.extensions === 'object';
}
#validateSpecV3() {
if (this.card.spec !== 'chara_card_v3') {
this.#lastValidationError = 'spec';
return false;
}
return true;
}
#validateSpecVersionV3() {
if (Number(this.card.spec_version) < 3.0 || Number(this.card.spec_version) >= 4.0) {
this.#lastValidationError = 'spec_version';
return false;
}
return true;
}
#validateDataV3() {
const data = this.card.data;
if (!data || typeof data !== 'object') {
this.#lastValidationError = 'No tavern card data found';
return false;
}
return true;
}
}
module.exports = { TavernCardValidator };
|