Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the LinkerAuthorization validator #246

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions report/schemas.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,30 @@ export namespace Link {
schema: JSONSchema<Link>;
}

// Warning: (ae-missing-release-tag) "LinkerAuthorization" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public
export type LinkerAuthorization = {
name: string;
desc: string;
startDate?: number;
endDate?: number;
contactInfo: {
name: string;
[key: string]: string;
};
addresses: string[];
plots: string[];
};

// @public (undocumented)
export namespace LinkerAuthorization {
const // (undocumented)
schema: JSONSchema<LinkerAuthorization>;
const // (undocumented)
validate: ValidateFunction<LinkerAuthorization>;
}

// @alpha
export type LinkUrl = string;

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export { World, ValidWorldRange, getWorld, isInsideWorldLimits } from './dapps/w
export * from './dapps/preview'
export * from './platform'
export * from './misc'
export * from './misc/linker-authorization'
export * from './misc/auth-chain'
export * from './misc/content-mapping'
export { sdk }
51 changes: 51 additions & 0 deletions src/misc/linker-authorization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { JSONSchema, ValidateFunction, generateLazyValidator } from '../validation'

/**
* Represents a Linker-Server Authorization.
*
* @public
*/
export type LinkerAuthorization = {
name: string
desc: string
startDate?: number
endDate?: number
contactInfo: {
name: string
[key: string]: string
}
addresses: string[]
plots: string[]
}

export namespace LinkerAuthorization {
export const schema: JSONSchema<LinkerAuthorization> = {
type: 'object',
properties: {
name: { type: 'string' },
desc: { type: 'string' },
startDate: { type: 'number', nullable: true },
endDate: { type: 'number', nullable: true },
contactInfo: {
type: 'object',
properties: {
name: { type: 'string' }
},
required: ['name']
},
addresses: {
type: 'array',
items: { type: 'string' },
minItems: 1
},
plots: {
type: 'array',
items: { type: 'string' },
minItems: 1
}
},
required: ['name', 'desc', 'contactInfo', 'addresses', 'plots']
}

export const validate: ValidateFunction<LinkerAuthorization> = generateLazyValidator(schema)
}
139 changes: 139 additions & 0 deletions test/misc/linker-authorization.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import expect from 'expect'
import { LinkerAuthorization } from '../../src'

describe('when validation authorizations', () => {
describe('and the authorization does not include a name', () => {
it('should return false', () => {
expect(
LinkerAuthorization.validate({
desc: 'aDesc',
contactInfo: {
name: 'aContactInfoName'
},
addresses: ['0x4730182099bc4e60075C657cCeCEc8879826bb43'],
plots: ['-73,50']
})
).toBe(false)
})
})

describe('and the authorization does not include a description', () => {
it('should return false', () => {
expect(
LinkerAuthorization.validate({
name: 'aName',
contactInfo: {
name: 'aContactInfoName'
},
addresses: ['0x4730182099bc4e60075C657cCeCEc8879826bb43'],
plots: ['-73,50']
})
).toBe(false)
})
})

describe('and the authorization does not include the contact information', () => {
it('should return false', () => {
expect(
LinkerAuthorization.validate({
name: 'aName',
desc: 'aDesc',
addresses: ['0x4730182099bc4e60075C657cCeCEc8879826bb43'],
plots: ['-73,50']
})
).toBe(false)
})
})

describe('and the authorization contains an empty contact information', () => {
it('should return false', () => {
expect(
LinkerAuthorization.validate({
name: 'aName',
desc: 'aDesc',
contactInfo: {},
addresses: ['0x4730182099bc4e60075C657cCeCEc8879826bb43'],
plots: ['-73,50']
})
).toBe(false)
})
})

describe('and the authorization does not include the addresses', () => {
it('should return false', () => {
expect(
LinkerAuthorization.validate({
name: 'aName',
desc: 'aDesc',
contactInfo: {
name: 'aContactInfoName'
},
plots: ['-73,50']
})
).toBe(false)
})
})

describe('and the authorization contains an empty list of addresses', () => {
it('should return false', () => {
expect(
LinkerAuthorization.validate({
name: 'aName',
desc: 'aDesc',
contactInfo: {
name: 'aContactInfoName'
},
addresses: [],
plots: ['-73,50']
})
).toBe(false)
})
})

describe('and the authorization does not include the plots', () => {
it('should return false', () => {
expect(
LinkerAuthorization.validate({
name: 'aName',
desc: 'aDesc',
contactInfo: {
name: 'aContactInfoName'
},
addresses: ['0x4730182099bc4e60075C657cCeCEc8879826bb43']
})
).toBe(false)
})
})

describe('and the authorization contains an empty list of plots', () => {
it('should return false', () => {
expect(
LinkerAuthorization.validate({
name: 'aName',
desc: 'aDesc',
contactInfo: {
name: 'aContactInfoName'
},
addresses: ['0x4730182099bc4e60075C657cCeCEc8879826bb43'],
plots: []
})
).toBe(false)
})
})

describe('and the authorization is formatted accordingly', () => {
it('should return true', () => {
expect(
LinkerAuthorization.validate({
name: 'aName',
desc: 'aDesc',
contactInfo: {
name: 'aContactInfoName'
},
addresses: ['0x4730182099bc4e60075C657cCeCEc8879826bb43'],
plots: ['-73,50']
})
).toBe(true)
})
})
})
Loading