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

Use standard and @fastify/pre-commit #190

Merged
merged 6 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 0 additions & 10 deletions .prettierrc

This file was deleted.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,35 @@
"lcovonly"
]
},
"lint-staged": {
"*.{json,md,js,ts,yml}": [
"prettier --write",
"git add"
]
},
"scripts": {
"lint": "standard | snazzy",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "jest src/*.test.js --verbose",
"test:coverage": "jest --coverage && npm run test:typescript",
"test:watch": "jest src/*.test.js --verbose --watch",
"test:typescript": "tsd",
"prepare": "husky install",
"format": "prettier --write ./src/**/*.js",
"coverage": "jest src/**.test.js --coverage",
"doc": "jsdoc2md ./src/*.js > docs/API.md"
},
"standard": {
"env": [ "jest" ]
},
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"ajv": "^8.0.0",
"ajv-formats": "^2.1.1",
"husky": "^8.0.1",
"jest": "^28.0.0",
"jsdoc-to-markdown": "^7.0.0",
"lint-staged": "^13.0.0",
"lodash.merge": "^4.6.2",
"prettier": "^2.2.1",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tsd": "^0.22.0"
},
"dependencies": {
"@fastify/deepmerge": "^1.1.0"
}
},
"pre-commit": [
"lint",
"test"
]
}
36 changes: 16 additions & 20 deletions src/ArraySchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const initialState = {
type: 'array',
definitions: [],
properties: [],
required: [],
required: []
}

/**
Expand All @@ -23,7 +23,7 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
options = {
generateIds: false,
factory: ArraySchema,
...options,
...options
}
return {
...BaseSchema({ ...options, schema }),
Expand All @@ -46,8 +46,7 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
Array.isArray(items) &&
items.filter(v => isFluentSchema(v)).length > 0
)
)
throw new FluentSchemaError("'items' must be a S or an array of S")
) { throw new FluentSchemaError("'items' must be a S or an array of S") }
if (Array.isArray(items)) {
const values = items.map(v => {
const { $schema, ...rest } = v.valueOf()
Expand All @@ -59,7 +58,7 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
return setAttribute({ schema, ...options }, [
'items',
{ ...rest },
'array',
'array'
])
},

Expand All @@ -72,22 +71,23 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
*/

additionalItems: items => {
if (typeof items !== 'boolean' && !isFluentSchema(items))
if (typeof items !== 'boolean' && !isFluentSchema(items)) {
throw new FluentSchemaError(
"'additionalItems' must be a boolean or a S"
)
}
if (items === false) {
return setAttribute({ schema, ...options }, [
'additionalItems',
false,
'array',
'array'
])
}
const { $schema, ...rest } = items.valueOf()
return setAttribute({ schema, ...options }, [
'additionalItems',
{ ...rest },
'array',
'array'
])
},

Expand All @@ -100,8 +100,7 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
*/

contains: value => {
if (!isFluentSchema(value))
throw new FluentSchemaError("'contains' must be a S")
if (!isFluentSchema(value)) { throw new FluentSchemaError("'contains' must be a S") }
const {
$schema,
definitions,
Expand All @@ -112,7 +111,7 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
return setAttribute({ schema, ...options }, [
'contains',
{ ...rest },
'array',
'array'
])
},

Expand All @@ -127,12 +126,11 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
*/

uniqueItems: boolean => {
if (typeof boolean !== 'boolean')
throw new FluentSchemaError("'uniqueItems' must be a boolean")
if (typeof boolean !== 'boolean') { throw new FluentSchemaError("'uniqueItems' must be a boolean") }
return setAttribute({ schema, ...options }, [
'uniqueItems',
boolean,
'array',
'array'
])
},

Expand All @@ -146,8 +144,7 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
*/

minItems: min => {
if (!Number.isInteger(min))
throw new FluentSchemaError("'minItems' must be a integer")
if (!Number.isInteger(min)) { throw new FluentSchemaError("'minItems' must be a integer") }
return setAttribute({ schema, ...options }, ['minItems', min, 'array'])
},

Expand All @@ -161,14 +158,13 @@ const ArraySchema = ({ schema = initialState, ...options } = {}) => {
*/

maxItems: max => {
if (!Number.isInteger(max))
throw new FluentSchemaError("'maxItems' must be a integer")
if (!Number.isInteger(max)) { throw new FluentSchemaError("'maxItems' must be a integer") }
return setAttribute({ schema, ...options }, ['maxItems', max, 'array'])
},
}
}
}

module.exports = {
ArraySchema,
default: ArraySchema,
default: ArraySchema
}
22 changes: 11 additions & 11 deletions src/ArraySchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ describe('ArraySchema', () => {
describe('constructor', () => {
it('without params', () => {
expect(ArraySchema().valueOf()).toEqual({
type: 'array',
type: 'array'
})
})

it('from S', () => {
expect(S.array().valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'array',
type: 'array'
})
})
})
Expand All @@ -34,7 +34,7 @@ describe('ArraySchema', () => {
.valueOf()
).toEqual({
type: 'array',
items: { type: 'number' },
items: { type: 'number' }
})
})
it('valid array', () => {
Expand All @@ -44,7 +44,7 @@ describe('ArraySchema', () => {
.valueOf()
).toEqual({
type: 'array',
items: [{ type: 'number' }, { type: 'string' }],
items: [{ type: 'number' }, { type: 'string' }]
})
})
it('invalid', () => {
Expand All @@ -64,7 +64,7 @@ describe('ArraySchema', () => {
).toEqual({
type: 'array',
items: [{ type: 'number' }, { type: 'string' }],
additionalItems: { type: 'string' },
additionalItems: { type: 'string' }
})
})
it('false', () => {
Expand All @@ -76,7 +76,7 @@ describe('ArraySchema', () => {
).toEqual({
type: 'array',
items: [{ type: 'number' }, { type: 'string' }],
additionalItems: false,
additionalItems: false
})
})
it('invalid', () => {
Expand All @@ -94,7 +94,7 @@ describe('ArraySchema', () => {
.valueOf()
).toEqual({
type: 'array',
contains: { type: 'string' },
contains: { type: 'string' }
})
})
it('invalid', () => {
Expand All @@ -114,7 +114,7 @@ describe('ArraySchema', () => {
.valueOf()
).toEqual({
type: 'array',
uniqueItems: true,
uniqueItems: true
})
})
it('invalid', () => {
Expand All @@ -136,7 +136,7 @@ describe('ArraySchema', () => {
.valueOf()
).toEqual({
type: 'array',
minItems: 3,
minItems: 3
})
})
it('invalid', () => {
Expand All @@ -156,7 +156,7 @@ describe('ArraySchema', () => {
.valueOf()
).toEqual({
type: 'array',
maxItems: 5,
maxItems: 5
})
})
it('invalid', () => {
Expand All @@ -176,7 +176,7 @@ describe('ArraySchema', () => {

expect(schema).toEqual({
type: 'array',
customKeyword: true,
customKeyword: true
})
})
})
Expand Down
Loading