Skip to content

Commit

Permalink
remove schema.error()
Browse files Browse the repository at this point in the history
  • Loading branch information
nuxodin committed Mar 31, 2023
1 parent 954f5e7 commit 0fdda79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 38 deletions.
48 changes: 16 additions & 32 deletions schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const AllSchemas = new Map();
function loadSchema(url) {
if (!AllSchemas.has(url)) {
const promise = fetch(url).then(res => res.json()).then(async data => {

if (data.$id && data.$id !== url) console.warn('Schema id does not match url', data.$id, url);
data.$id = url;

Expand Down Expand Up @@ -58,7 +57,7 @@ export class Schema {
* @returns {boolean} True if the value is valid, false otherwise.
*/
validate(value) {
return !this.error(value);
return !this.error(value).next().done;
}

/**
Expand All @@ -73,15 +72,6 @@ export class Schema {
return yield* errors(value, this.schema);
}

/**
* Returns the first error for the given value.
* @param {*} value - The value to check for an error.
* @returns {object|boolean} The first error or false if no errors are found.
*/
error(value) {
return this.errors(value).next().value??false;
}

#findAnchors(schema) {
if (schema.$id && schema.$id !== this.id) return;
if (schema.$anchor && !this.anchors.has(schema.$anchor)) this.anchors.set(schema.$anchor, schema);
Expand Down Expand Up @@ -745,36 +735,30 @@ function isValidHostname(hostname) {
}
return true;
}



function isValidIdnHostname(hostname) {
try { new URL('http://' + hostname); } catch { return false; }
return !hostname.split('.').some((label) => {
if (label.length > 63) return true;
if (label.substring(2, 4) === '--') return true;
if (label.startsWith('-') || label.endsWith('-')) return true;
for (let label of hostname.split('.')) {
label = label.toLowerCase();

if (label.length > 63) return false;
if (label.substring(2, 4) === '--') return false;
if (label.startsWith('-') || label.endsWith('-')) return false;
// Hebrew GERSHAYIM not preceded by anything
if (label.match(/(?<!.)\u05F4/)) return true;
if (label.match(/(?<!.)\u05F4/)) return false;
// Hebrew GERESH not preceded by Hebrew
if (label.match(/(?<![\p{Script=Hebrew}])\u05F3/u)) return true;
if (label.match(/(?<![\p{Script=Hebrew}])\u05F3/u)) return false;
// Greek KERAIA not followed by Greek
if (label.match(/\u0375(?![\p{Script=Greek}])/u)) return true;
if (label.match(/\u0375(?![\p{Script=Greek}])/u)) return false;
// Hangul Tone Mark
if (label.includes('\u302E')) return true;
// Japanese middle dot
if (label.includes('・')) {
if (label.includes('\u302E')) return false;
// Japanese middle dot and Interpunct (middle dot) only allowed when other Hiragana, Katakana or Han characters are present
if (label.includes('・') || label.includes('·')) {
if (!/[\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}]/u.test(label)) {
return true;
return false;
}
}
// Interpunct (middle dot)
if (label.includes('·')) {
if (!/[\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}]/u.test(label)) {
return true;
}
}
});
}
return true;
}
function isValidIPv4(ip) {
return /^((?!0\d)\d{1,3}\.){3}(?!0\d)\d{1,3}$/.test(ip) && ip.split('.').every(p => p >= 0 && p <= 255);
Expand Down
2 changes: 1 addition & 1 deletion tests/test-suite-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ let files = [

/* *
files = [
'dynamicRef.json',
//'dynamicRef.json',
];
/* */

Expand Down
5 changes: 0 additions & 5 deletions tests/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
const schema = new Schema({ type: 'number', minimum: 3});
chai.expect( schema.validate('s') ).to.be.equal(false);
});
it('schema.error()', () => {
const schema = new Schema({ type: 'integer', minimum: 3});
chai.expect( schema.error(3.3) ).to.be.an('string');
chai.expect( schema.error(3) ).to.be.equal(false);
});
it('schema.errors()', () => {
const schema = new Schema({ type: 'integer', minimum: 3});
chai.expect( [...schema.errors(2.5)].length ).to.be.equal(2);
Expand Down

0 comments on commit 0fdda79

Please sign in to comment.