esp's notebooks

  • Ajv issue - /esp/ajv-issue
    Last edited 3 years ago
    const Ajv = require('ajv').default; ajv = new Ajv({ // options here }); const schema = { }; const data = { }; const validate = ajv.compile(schema); console.log(validate(data)); console.log(validate.errors);
  • Ajv - Asynchronous validation - /esp/ajv-asynchronous-validation
    Last edited 3 years ago
    const Ajv = require('ajv').default; const ajv = new Ajv({allErrors: true}); const request = require("request"); ajv.addFormat('instagram', { async: true, validate: (username) => new Promise((resolve, reject) => { console.log(username) request.get('https://www.instagram.com/' + username + "/", (err, res) => { console.log(res) if (err) reject(err); else if (res.statusCode >= 400) reject(new Error(`${res.statusCode}`)) else resolve(true) }); }) }); const schema = { $async: true, type: "object", properties: { instagram_username: { type: "string", format: "instagram" } } }; const validate = ajv.compile(schema); test({instagram_username: 'wikipedia'}) .then(() => { test({instagram_username: 'no_such_user_name'}) }); async function test(data) { try { console.log(await validate(data)) console.log('valid data: ' + JSON.stringify(data)) } catch(err) { if (err instanceof Ajv.ValidationError) { console.log('invalid data: ' + JSON.stringify(data), 'errors:', err.errors) } else { console.log(err) } } }
  • bounding keyword (v5 proposal) - switch as an alternative - /esp/bounding-alternative
    Last edited 7 years ago
    var schema = { "id": "http://some.site.somewhere/entry-schema#", "$schema": "http://json-schema.org/draft-04/schema#", "description": "schema for an fstab entry", "type": "object", "required": [ "record" ], "properties": { "record": { "type": "object", "oneOf": [ { "$ref": "#/definitions/student" }, { "$ref": "#/definitions/book" } ] } }, "definitions": { "student": { "properties": { "type": { "enum": [ "student" ], "bounding": true }, "age": { "type": "integer" } }, "required": [ "age" ], "additionalProperties": false }, "book": { "properties": { "type": { "enum": [ "book" ], "bounding": true }, "pages": { "type": "integer" } }, "required": [ "pages" ], "additionalProperties": false } } }; var data = { "record": { "type": "student" } }; var ajv = require('ajv')({allErrors: true, v5: true}); console.log(ajv.validate(schema, data)); console.log(ajv.errorsText()); var schema2 = { "type": "object", "required": [ "record" ], "properties": { "record": { "type": "object", "switch": [ { "if": { "properties": { "type": { "constant": "student" } } }, "then": { "$ref": "#/definitions/student" } }, { "if": { "properties": { "type": { "constant": "student" } } }, "then": { "$ref": "#/definitions/book" } }, { "then": false } ] } }, "definitions": { "student": { "properties": { "type": {}, "age": { "type": "integer" } }, "required": [ "age" ], "additionalProperties": false }, "book": { "properties": { "type": {}, "pages": { "type": "integer" } }, "required": [ "pages" ], "additionalProperties": false } } }; console.log(ajv.validate(schema2, data)); console.log(ajv.errorsText());
  • tuts+. JSON-schema validation, part1. items/additionalItems - /esp/tutsplus-json-schema-validation-part1-items-and-additionalitems
    Last edited 7 years ago
    var Ajv = require('ajv'); var ajv = Ajv({allErrors: true}); var schema = { "type": "array", "items": [ { "type": "integer" }, { "type": "string" } ] }; var validate = ajv.compile(schema); console.log(validate([1, "foo"])); console.log(validate([1, "foo", {"bar": "baz"}])); validate(["foo", "bar"]);
  • tuts+. JSON-schema validation, part1. properties & required - /esp/tutsplus-json-schema-validation-part1-properties-and-required
    Last edited 7 years ago
    var Ajv = require('ajv'); var ajv = Ajv({allErrors: true}); var schema = { "properties": { "foo": { "type": "string" } } }; var validate = ajv.compile(schema); console.log(validate({foo: 'bar'})); console.log(validate({foo: 'bar', baz: 'bax'})); console.log(validate({baz: 'bax'})); validate({});
  • tuts+. JSON-schema validation, part1. User example 2 - /esp/tutsplus-json-schema-validation-part1-user-example2
    Last edited 7 years ago
    var user = { "id": 64209690, "name": "Jane Smith", "email": "jane.smith@gmail.com", "phone": "07777 888 999", "address": { "street": "Flat 1, 188 High Street Kensington", "postcode": "W8 5AA", "city": "London", "country": "United Kingdom" }, "personal": { "DOB": "1982-08-16", "age": 33, "gender": "female" }, "connections": [ { "id": "35434004285760", "name": "John Doe", "connType": "friend", "since": "2014-03-25" }, { "id": 13418315, "name": "James Smith", "connType": "relative", "relation": "husband", "close": true, "since": "2012-07-03" } ], "feeds": { "news": true, "sport": true, "fashion": false }, "createdAt": "2015-09-22T10:30:06.000Z" }; var userSchema = { "$schema": "http://json-schema.org/draft-04/schema#", "id": "http://mynet.com/schemas/user.json#", "title": "User", "description": "User profile with connections", "type": "object", "properties": { "id": { "type": ["string", "integer"], "pattern": "^[1-9][0-9]*quot;, "minimum": 1 }, "name": { "type": "string", "maxLength": 128 }, "email": { "type": "string", "format": "email" }, "phone": { "type": "string", "pattern": "^[0-9()\\-\\.\\s]+quot; }, "address": { "type": "object", "additionalProperties": { "type": "string" }, "maxProperties": 6, "required": ["street", "postcode", "city", "country"] }, "personal": { "type": "object", "properties": { "DOB": { "type": "string", "format": "date" }, "age": { "type": "number", "minimum": 13 }, "gender": { "enum": ["female", "male"] }, }, "additionalProperties": false, "required": ["DOB", "age"] }, "connections": { "type": "array", "maxItems": 150, "items": { "type": "object", "properties": { "id": { "type": ["string", "integer"], "pattern": "^[1-9][0-9]*quot;, "minimum": 1 }, "name": { "type": "string", "maxLength": 128 }, "since": { "type": "string", "format": "date" }, "connType": { "type": "string" }, "relation": {}, "close": {} }, "oneOf": [ { "properties": { "connType": { "enum": ["relative"] }, "relation": { "type": "string" } }, "dependencies": { "relation": { "properties": { "close": { "type": "boolean" }, }, "required": ["close"] } } }, { "properties": { "connType": { "enum": ["friend", "colleague", "other"] }, "relation": { "not": {} }, "close": { "not": {} } }, } ], "required": ["id", "name", "since", "connType"], "additionalProperties": false } }, "feeds": { "type": "object", "patternProperties": { "^[A-Za-z]+quot;: { "type": "boolean" } }, "additionalProperties": false }, "createdAt": { "type": "string", "format": "date-time" } } }; var Ajv = require('ajv'); var ajv = Ajv({allErrors: true}); var validate = ajv.compile(userSchema); var valid = validate(user); if (valid) { console.log('User data is valid'); } else { console.log('User data is INVALID!'); console.log(validate.errors); }
  • tuts+, JSON-schema validation, part1. User example - /esp/tutsplus-json-schema-validation-part1-user-example
    Last edited 7 years ago
    var user = { "id": 64209690, "name": "Jane Smith", "email": "jane.smith@gmail.com", "phone": "07777 888 999", "address": { "street": "Flat 1, 188 High Street Kensington", "postcode": "W8 5AA", "city": "London", "country": "United Kingdom" }, "personal": { "DOB": "1982-08-16", "age": 33, "gender": "female" }, "connections": [ { "id": "35434004285760", "name": "John Doe", "connType": "friend", "since": "2014-03-25" }, { "id": 13418315, "name": "James Smith", "connType": "relative", "relation": "husband", "close": true, "since": "2012-07-03" } ], "feeds": { "news": true, "sport": true, "fashion": false }, "createdAt": "2015-09-22T10:30:06.000Z" }; var userSchema = { "$schema": "http://json-schema.org/draft-04/schema#", "id": "http://mynet.com/schemas/user.json#", "title": "User", "description": "User profile with connections", "type": "object", "properties": { "id": { "type": ["string", "integer"], "pattern": "^[1-9][0-9]*quot;, "minimum": 1 }, "name": { "type": "string", "maxLength": 128 }, "email": { "type": "string", "format": "email" }, "phone": { "type": "string", "pattern": "^[0-9()\\-\\.\\s]+quot; }, "address": { "type": "object", "additionalProperties": { "type": "string" }, "maxProperties": 6, "required": ["street", "postcode", "city", "country"] }, "personal": { "type": "object", "properties": { "DOB": { "type": "string", "format": "date" }, "age": { "type": "number", "minimum": 13 }, "gender": { "enum": ["female", "male"] }, }, "additionalProperties": false, "required": ["DOB", "age"] }, "connections": { "type": "array", "maxItems": 150, "items": { "type": "object", "properties": { "id": { "type": ["string", "integer"], "pattern": "^[1-9][0-9]*quot;, "minimum": 1 }, "name": { "type": "string", "maxLength": 128 }, "since": { "type": "string", "format": "date" }, "connType": { "type": "string" }, "relation": {}, "close": {} }, "oneOf": [ { "properties": { "connType": { "enum": ["relative"] }, "relation": { "type": "string" } }, "dependencies": { "relation": ["close"] } }, { "properties": { "connType": { "enum": ["friend", "colleague", "other"] }, "relation": { "not": {} }, "close": { "not": {} } }, } ], "required": ["id", "name", "since", "connType"], "additionalProperties": false } }, "feeds": { "type": "object", "patternProperties": { "^[A-Za-z]+quot;: { "type": "boolean" } }, "additionalProperties": false }, "createdAt": { "type": "string", "format": "date-time" } } }; var Ajv = require('ajv'); var ajv = Ajv({allErrors: true}); var validate = ajv.compile(userSchema); var valid = validate(user); if (valid) { console.log('User data is valid'); } else { console.log('User data is INVALID!'); console.log(validate.errors); }
  • Ajv - the fastest JSON-schema validator - /esp/ajv
    Last edited 7 years ago
    var Ajv = require('ajv'); var ajv = Ajv({allErrors: true}); var validate = ajv.compile({ "properties": { "foo": { "type": "string" }, "bar": { "type": "number", "maximum": 3 } } }); test({"foo": "abc", "bar": 2}); test({"foo": 2, "bar": 4}); function test(data) { var valid = validate(data); if (valid) console.log('Valid!'); else console.log('Invalid:', ajv.errorsText(validate.errors)); }