Skip to content

Commit

Permalink
Merge pull request #548 from keystonejs/is-boolean
Browse files Browse the repository at this point in the history
[DISCUSS] Replace unique -> isUnique and required -> isRequired
  • Loading branch information
timleslie authored Dec 6, 2018
2 parents 93e1caf + 78fd955 commit f587637
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .changeset/5d86a313/changes.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Enable sorting on DateTime fields
- Enable sorting on DateTime fields
31 changes: 31 additions & 0 deletions .changeset/79365e10/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"releases": [{ "name": "@voussoir/core", "type": "major" }],
"dependents": [
{ "name": "@voussoir/adapter-mongoose", "type": "patch", "dependencies": ["@voussoir/core"] },
{
"name": "@voussoir/test-utils",
"type": "patch",
"dependencies": ["@voussoir/adapter-mongoose", "@voussoir/core"]
},
{
"name": "@voussoir/cypress-project-access-control",
"type": "patch",
"dependencies": ["@voussoir/adapter-mongoose", "@voussoir/test-utils", "@voussoir/core"]
},
{
"name": "@voussoir/cypress-project-basic",
"type": "patch",
"dependencies": ["@voussoir/adapter-mongoose", "@voussoir/test-utils", "@voussoir/core"]
},
{
"name": "@voussoir/cypress-project-login",
"type": "patch",
"dependencies": ["@voussoir/adapter-mongoose", "@voussoir/test-utils", "@voussoir/core"]
},
{
"name": "@voussoir/cypress-project-twitter-login",
"type": "patch",
"dependencies": ["@voussoir/adapter-mongoose", "@voussoir/core"]
}
]
}
1 change: 1 addition & 0 deletions .changeset/79365e10/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Field configuration now tasks isRequired and isUnique, rather than required and unique
48 changes: 48 additions & 0 deletions .changeset/a0b53437/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"releases": [
{ "name": "@voussoir/adapter-mongoose", "type": "major" },
{ "name": "@voussoir/fields", "type": "major" },
{ "name": "@voussoir/cypress-project-basic", "type": "minor" }
],
"dependents": [
{
"name": "@voussoir/test-utils",
"type": "patch",
"dependencies": ["@voussoir/core", "@voussoir/adapter-mongoose"]
},
{
"name": "@voussoir/cypress-project-access-control",
"type": "patch",
"dependencies": [
"@voussoir/test-utils",
"@voussoir/admin-ui",
"@voussoir/core",
"@voussoir/adapter-mongoose",
"@voussoir/fields"
]
},
{
"name": "@voussoir/cypress-project-login",
"type": "patch",
"dependencies": [
"@voussoir/test-utils",
"@voussoir/admin-ui",
"@voussoir/core",
"@voussoir/adapter-mongoose",
"@voussoir/fields"
]
},
{
"name": "@voussoir/cypress-project-twitter-login",
"type": "patch",
"dependencies": [
"@voussoir/admin-ui",
"@voussoir/core",
"@voussoir/adapter-mongoose",
"@voussoir/fields"
]
},
{ "name": "@voussoir/admin-ui", "type": "patch", "dependencies": ["@voussoir/fields"] },
{ "name": "@voussoir/core", "type": "patch", "dependencies": ["@voussoir/fields"] }
]
}
1 change: 1 addition & 0 deletions .changeset/a0b53437/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Field configuration now tasks isRequired and isUnique, rather than required and unique
2 changes: 1 addition & 1 deletion docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Text extends Implementation {
keystone.createList('User', {
fields: {
name: { type: Text, minLength: 3 },
email: { type: Text, required: true, unique: true },
email: { type: Text, isRequired: true, isUnique: true },
pass: { type: Password, rejectCommon: true },
},

Expand Down
8 changes: 4 additions & 4 deletions packages/adapter-mongoose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,11 @@ class MongooseFieldAdapter extends BaseFieldAdapter {
addToMongooseSchema() {
throw new Error(`Field type [${this.fieldName}] does not implement addToMongooseSchema()`);
}
buildValidator(validator, required) {
return required ? validator : a => validator(a) || typeof a === 'undefined' || a === null;
buildValidator(validator, isRequired) {
return isRequired ? validator : a => validator(a) || typeof a === 'undefined' || a === null;
}
mergeSchemaOptions(schemaOptions, { unique, mongooseOptions }) {
if (unique) {
mergeSchemaOptions(schemaOptions, { isUnique, mongooseOptions }) {
if (isUnique) {
// A value of anything other than `true` causes errors with Mongoose
// constantly recreating indexes. Ie; if we just splat `unique` onto the
// options object, it would be `undefined`, which would cause Mongoose to
Expand Down
4 changes: 2 additions & 2 deletions packages/fields/types/CalendarDay/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class CalendarDay extends Implementation {
class MongoCalendarDayInterface extends MongooseFieldAdapter {
addToMongooseSchema(schema) {
const { mongooseOptions = {} } = this.config;
const { required } = mongooseOptions;
const { isRequired } = mongooseOptions;

const validator = a => typeof a === 'string' && format(parse(a), 'YYYY-MM-DD') === a;
const schemaOptions = {
type: String,
validate: {
validator: this.buildValidator(validator, required),
validator: this.buildValidator(validator, isRequired),
message: '{VALUE} is not an ISO8601 date string (YYYY-MM-DD)',
},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/fields/types/Decimal/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class Decimal extends Implementation {
class MongoDecimalInterface extends MongooseFieldAdapter {
addToMongooseSchema(schema) {
const { mongooseOptions = {} } = this.config;
const { required } = mongooseOptions;
const { isRequired } = mongooseOptions;

const validator = a => typeof a === 'object' && /^-?\d*\.?\d*$/.test(a);
const schemaOptions = {
type: mongoose.Decimal128,
validate: {
validator: this.buildValidator(validator, required),
validator: this.buildValidator(validator, isRequired),
message: '{VALUE} is not a Decimal value',
},
};
Expand Down
7 changes: 5 additions & 2 deletions packages/fields/types/Integer/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ class Integer extends Implementation {
class MongoIntegerInterface extends MongooseFieldAdapter {
addToMongooseSchema(schema) {
const { mongooseOptions = {} } = this.config;
const { required } = mongooseOptions;
const { isRequired } = mongooseOptions;

const schemaOptions = {
type: Number,
validate: {
validator: this.buildValidator(a => typeof a === 'number' && Number.isInteger(a), required),
validator: this.buildValidator(
a => typeof a === 'number' && Number.isInteger(a),
isRequired
),
message: '{VALUE} is not an integer value',
},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/fields/types/Relationship/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ class Relationship extends Implementation {
}

async resolveRelationship(input, item, context, getItem, mutationState) {
const { many, required } = this.config;
const { many, isRequired } = this.config;

// Early out for null'd field
if (!required && !input) {
if (!isRequired && !input) {
return input;
}

Expand Down
2 changes: 1 addition & 1 deletion projects/basic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ try {
keystone.createList('User', {
fields: {
name: { type: Text },
email: { type: Text, unique: true },
email: { type: Text, isUnique: true },
dob: {
type: CalendarDay,
format: 'Do MMMM YYYY',
Expand Down
2 changes: 1 addition & 1 deletion projects/basic/tests/uniqueness/unique.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function setupKeystone() {
keystone.createList('User', {
fields: {
username: { type: Text },
email: { type: Text, unique: true },
email: { type: Text, isUnique: true },
},
});
},
Expand Down

0 comments on commit f587637

Please sign in to comment.